Java 类设计----Java类的继承

时间:2021-04-12 03:50:37

Java类的继承

为描述和处理个人信息,定义类Person:
public class Person {
  public String name;
  public inat age;
  public Date birthDate;

  public String getInfo()
  {...}
}
为描述和处理学生信息,定义类Student:
public class Student {
  public String name;
  public int age;
  public Date birthDate;
  public String school;

  public String getInfo()
  {...}
}
通过继承,简化Student类的定义:
public class Person {
  public String name;
  public int age;
  public Date birthDate;
  public String getInfo() {...}
}

public class Student extends Person{
  public String school;
}
Student类继承了父类Person的所有属性和方法,并增加了一个属性school。Person中的属性和方法,Student都可以利用。
类继承语法规则:

  < 修饰符> class < 子类名称> [extends < 父类>]
  {
    <属性和方法的声明>
  }
Java只支持单继承,不允许多重继承
一个子类只能有一个父类,一个父类可以派生出多个子类
子类继承了父类,就继承了父类的方法和属性。
在子类中,可以使用父类中定义的方法和属性,也可以创建新的数据和方法。

在Java 中,继承的关键字用的是“extends”,即子类不是父类的子集,而是对父类的“扩展”。

关于继承的规则:
  子类不能继承父类中私有的(private)的成员变量和方法

例子:

public class Person {
public String name;
public int age;
public Date birth;
public String getInfo(){
return "name: " + name + ", " + "age: " + age + ", " + "birth: " + birth;
}
}
public class Student extends Person{

    public String school;

        public void print() {
//System.out.println(this.lover);
}
}
public class TestPerson {
public static void main(String[] args) { Student student = new Student();
student.name = "Jerry";
student.birth = new Date();
student.age = 1;
student.school = "atguigu"; // student.lover = ""; System.out.println(student.getInfo()); Person person = new Person(); person.age = 1;
person.birth = new Date();
person.name = "Tom"; System.out.println(person.getInfo());
}
}

练习
1、(1)定义一个ManKind类,包括
  成员变量 int sexint salary
  方法 void manOrWorman():根据sex的值显示“man”(sex==1)或者“women”(sex==0);
  方法 void employeed():根据salary的值显示“no job”(salary==0)或者“ job”(salary!=0)。

 /**
* 定义一个ManKind类,包括
* 成员变量 int sex 和 int salary;
* 方法 void manOrWorman():根据sex的值显示“man”(sex==1)或者“women”(sex==0);
* 方法 void employeed():根据salary的值显示“no job”(salary==0)或者“ job”(salary!=0)。
*/
public class ManKind { int sex;
int salary; public void manOrWoman() {
if(sex == 0){
System.out.println("woman");
}else if(sex == 1){
System.out.println("man");
}
} public void employeed() {
if(salary != 0){
System.out.println("job");
}else{
System.out.println("no job");
}
}
}

(2)定义类Kids1继承ManKind,并包括
  成员变量 int yearsOld;
  方法 printAge() 打印 yearsOld 的值。

(3)在Kids1类的main方法中实例化Kids1的对象 someKid,用该对象访问其父类的成员变量及方法。

 /**
* 定义类 Kids1 继承ManKind,并包括
* 成员变量 int yearsOld;
* 方法 printAge() 打印 yearsOld 的值。
*
* 在Kids1中重新定义employed() 方法,覆盖父类ManKind中定义的employed()方法,
* 输出“Kids should study and no job.”
*/
public class Kids1 extends ManKind{ int yearsOld; void printAge(){
System.out.println("yearsOld: " + yearsOld);
} //修改练习3中定义的类Kids1中employed()方法,在该方法中调用父类ManKind的employed()方法,
//然后再输出“but Kids should study and no job.”
public void employeed() {
super.employeed();
System.out.println("Kids should study and no job.");
} //在Kids1类的main方法中实例化Kids1的对象 someKid,用该对象访问其父类的成员变量及方法。
public static void main(String[] args) {
Kids1 someKid = new Kids1();
someKid.sex = 1;
someKid.salary = 5000;
someKid.yearsOld = 25; someKid.manOrWoman();
someKid.employeed();
someKid.printAge();
}
}

2、根据下图实现类。在TestCylinder类中创建Cylinder类的对象,设置圆柱的底面半径和高,并输出圆柱的体积。

Java 类设计----Java类的继承

 public class Circle {

     protected double radius;

     public Circle(double radius) {
this.radius = radius;
} public double getRadius() {
return radius;
} public void setRadius(double radius) {
this.radius = radius;
} public double findArea(){
return 3.14 * radius * radius;
}
}
 public class Cylinder extends Circle{

     private double length;

      public Cylinder() {
this.length = 1;
} public double getLength() {
return length;
} public void setLength(double length) {
this.length = length;
} /**
* 返回圆柱的体积
* @return
*/
public double findVolume(){
return super.findArea() * length;
} /**
* 返回圆柱的表面积
*/
@Override
public double findArea() {
return super.findArea() * 2 + 2 * 3.14 * radius * length;
}
}
 public class TestCylinder {
public static void main(String[] args) { Cylinder cylinder = new Cylinder(); cylinder.setLength(2); //返回表面积
System.out.println(cylinder.findArea());
//返回体积
System.out.println(cylinder.findVolume());
}
}