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

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)。

 1 /**
 2  * 定义一个ManKind类,包括
 3  * 成员变量 int sex 和 int salary;
 4  * 方法 void manOrWorman():根据sex的值显示“man”(sex==1)或者“women”(sex==0);
 5  * 方法 void employeed():根据salary的值显示“no job”(salary==0)或者“ job”(salary!=0)。
 6  */
 7 public class ManKind {
 8 
 9     int sex;
10     int salary;
11     
12     public void manOrWoman() {
13         if(sex == 0){
14             System.out.println("woman");
15         }else if(sex == 1){
16             System.out.println("man");
17         }
18     }
19     
20     public void employeed() {
21         if(salary != 0){
22             System.out.println("job");
23         }else{ 
24             System.out.println("no job");
25         }
26     }
27 }


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

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

 1 /**
 2  * 定义类 Kids1 继承ManKind,并包括
 3  * 成员变量 int yearsOld;
 4  * 方法 printAge() 打印 yearsOld 的值。
 5  * 
 6  * 在Kids1中重新定义employed() 方法,覆盖父类ManKind中定义的employed()方法,
 7  * 输出“Kids should study and no job.”
 8  */
 9 public class Kids1 extends ManKind{
10 
11     int yearsOld;
12     
13     void printAge(){ 
14         System.out.println("yearsOld: " + yearsOld);
15     }
16     
17     //修改练习3中定义的类Kids1中employed()方法,在该方法中调用父类ManKind的employed()方法,
18     //然后再输出“but Kids should study and no job.”
19     public void employeed() {
20         super.employeed();
21         System.out.println("Kids should study and no job.");
22     }
23     
24     //在Kids1类的main方法中实例化Kids1的对象 someKid,用该对象访问其父类的成员变量及方法。
25     public static void main(String[] args) {
26         Kids1 someKid = new Kids1();
27         someKid.sex = 1;
28         someKid.salary = 5000;
29         someKid.yearsOld = 25;
30         
31         someKid.manOrWoman();
32         someKid.employeed();
33         someKid.printAge();
34     }
35 }

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

 1 public class Circle {
 2 
 3     protected double radius;
 4 
 5     public Circle(double radius) {
 6         this.radius = radius;
 7     }
 8     
 9     public double getRadius() {
10         return radius;
11     }
12 
13     public void setRadius(double radius) {
14         this.radius = radius;
15     }
16     
17     public double findArea(){
18         return 3.14 * radius * radius;
19     }
20 }
 1 public class Cylinder extends Circle{
 2 
 3     private double length;
 4 
 5      public Cylinder() {
 6         this.length = 1;
 7     }       
 8 
 9     public double getLength() {
10         return length;
11     }
12 
13     public void setLength(double length) {
14         this.length = length;
15     }
16     
17     /**
18      * 返回圆柱的体积
19      * @return
20      */
21     public double findVolume(){
22         return super.findArea() * length;
23     }
24     
25     /**
26      * 返回圆柱的表面积
27      */
28     @Override
29     public double findArea() {
30         return super.findArea() * 2 + 2 * 3.14 * radius * length;
31     }
32 }
 1 public class TestCylinder {
 2     public static void main(String[] args) {
 3         
 4         Cylinder cylinder = new Cylinder();
 5         
 6         cylinder.setLength(2);
 7         
 8         //返回表面积
 9         System.out.println(cylinder.findArea());
10         //返回体积
11         System.out.println(cylinder.findVolume());
12     }
13 }
原文地址:https://www.cnblogs.com/justdoitba/p/7223311.html