练习
练习1:
定义一个学生类Student,它继承自Person类
练习2:
(1)定义一个ManKind类,包括
-
成员变量int sex和int salary;
-
方法void manOrWoman():根据sex的值显示“man”(sex==1)或者“woman”(sex==0);
-
方法void employeed():根据salary的值显示“no job”(salary==0)或者“ job”(salary!=0)。
(2)定义类Kids继承ManKind,并包括
-
成员变量int yearsOld;
-
方法printAge()打印yearsOld的值。
(3)定义类KidsTest,在类的main方法中实例化Kids的对象someKid,用该对象访问其父类的成员变量及方法。
package chapter07_oop2_teacher.src.com.atguigu03._extends.exer1; /** * ClassName: ManKind * Description: * 定义一个ManKind类,包括 * * - 成员变量int sex和int salary; * - 方法void manOrWoman():根据sex的值显示“man”(sex==1)或者“woman”(sex==0); * - 方法void employeed():根据salary的值显示“no job”(salary==0)或者“ job”(salary!=0)。 * @Author 尚硅谷-宋红康 * @Create 9:50 * @Version 1.0 */ public class ManKind { private int sex; private int salary; public ManKind() { } public ManKind(int sex, int salary) { this.sex = sex; this.salary = salary; } public int getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } public void manOrWoman(){ if(sex == 1){ System.out.println("Man"); }else if(sex == 0){ System.out.println("Woman"); } } public void employeed(){ if(salary == 0){ System.out.println("no job!"); }else{ System.out.println("job!"); } } }
package chapter07_oop2_teacher.src.com.atguigu03._extends.exer1; /** * ClassName: KidsTest * Description: * 定义类KidsTest,在类的main方法中实例化Kids的对象someKid,用该对象访问其父类的成员变量及方法。 * @Author 尚硅谷-宋红康 * @Create 9:55 * @Version 1.0 */ public class KidsTest { public static void main(String[] args) { Kids kid = new Kids(); kid.setSex(1); kid.setSalary(100); kid.setYearsOld(12); //来自于父类中声明的方法 kid.employeed(); kid.manOrWoman(); //Kids类自己声明的方法 kid.printAge(); } }
package chapter07_oop2_teacher.src.com.atguigu03._extends.exer1; /** * ClassName: Kids * Description: * 定义类Kids继承ManKind,并包括 * * - 成员变量int yearsOld; * - 方法printAge()打印yearsOld的值。 * @Author 尚硅谷-宋红康 * @Create 9:52 * @Version 1.0 */ public class Kids extends ManKind{ private int yearsOld; public Kids(){ } public Kids(int yearsOld){ this.yearsOld = yearsOld; } public Kids(int sex, int salary,int yearsOld){ this.yearsOld = yearsOld; setSex(sex); setSalary(salary); } public int getYearsOld() { return yearsOld; } public void setYearsOld(int yearsOld) { this.yearsOld = yearsOld; } public void printAge(){ System.out.println("I am " + yearsOld + " years old."); } }
练习3:
根据下图实现类。在CylinderTest类中创建Cylinder类的对象,设置圆柱的底面半径和高,并输出圆柱的体积。
package chapter07_oop2_teacher.src.com.atguigu03._extends.exer2; /** * ClassName: Circle * Description: * * @Author 尚硅谷-宋红康 * @Create 10:00 * @Version 1.0 */ public class Circle { private double radius;//半径 public Circle(){ this.radius = 1; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } //求圆的面积 public double findArea(){ return 3.14 * radius * radius; } }
package chapter07_oop2_teacher.src.com.atguigu03._extends.exer2; /** * ClassName: Cylinder * Description: * 圆柱类 * @Author 尚硅谷-宋红康 * @Create 10:01 * @Version 1.0 */ public class Cylinder extends Circle{ private double length;//高 public Cylinder(){ length = 1; } public double getLength() { return length; } public void setLength(double length) { this.length = length; } //求圆柱的体积 public double findVolume(){ // return 3.14 * getRadius() * getRadius() * getLength(); return findArea() * getLength(); } }
package chapter07_oop2_teacher.src.com.atguigu03._extends.exer2; /** * ClassName: CylinderTest * Description: * * @Author 尚硅谷-宋红康 * @Create 10:04 * @Version 1.0 */ public class CylinderTest { public static void main(String[] args) { Cylinder cy = new Cylinder(); cy.setRadius(2.3); cy.setLength(1.4); System.out.println("圆柱的体积为:" + cy.findVolume()); System.out.println("圆柱的底面积:" + cy.findArea()); } }