[JAVA实验三]继承与多态

1.继承

1.1 继承的概念

  • 继承顾名思义,就是指从上一代传承下来的东西,可以自己添加其他任何方法,并实现各种新功能

1.2 继承的特点

  1. Java中,继承只能单一继承,也就是一个子类无法同时继承多个父类,但是可以多层继承。
  2. 成员变量依从就近原则,局部变量 > 子类的成员变量 > 父类的成员变量
  3. 子类只能获取父类的非私有成员

2.什么是多态

2.1 多态的概念

用通俗的话来解释,多态就是指多种形态
不同的对象去完成某个行为时会产生出不同的状态

3.例子内容

1.Animal类

设计 Animal 类。类中定义一个成员变量 animalName,表示动物的名称,变量 animalName 是 String 类型,默认初始值。在该类中再定义一个成员方法 shout(),表示动物 发出叫声的行为,该方法能输出名称为 animalName 的动物发出叫声

public class Animal {
    String animalName;
    public void shout() {
        System.out.println(animalName + "发出了叫声~");
    }
}

2.Cat类

设计 Cat 类。使用关键字 extends 继承 Animal 类。Cat 类中定义一个 public 类型的 构造方法,该构造方法拥有一个 String 类型的参数 catName,通过该参数,为 Cat 类中继承 自 Animal 类的 animalName 成员变量赋值。

public class Cat extends Animal{
    public Cat(String catName) {
        this.animalName = catName;
    }
}

3.ExtendsTest类

3.1

设计一个测试类。类名为 ExtendsTest,在 main 方法中定义一个 String 类型的 name 变量,并为它赋值为“波斯猫”,然后使用有参构造创建 Cat 类的对象,并使用对象的引用 变量调用 shout()方法。

public class ExtendsTest {
    public static void main(String[] args) {
        String name = "波斯猫";
        Cat cat = new Cat(name);
        cat.shout();
    }
}

3.2

设计父类和一个子类,在子类里面通过 super 关键字去调用父类的成员变量和成 员方法,在子类的构造方法中使用 super 关键字调用父类的构造方法,在测试类里进行验证   

3.2.1 Animal类(修改)

public class Animal {
    String animalName;
    String type = "哺乳类动物";
    public void shout() {
        System.out.println(animalName + "发出了叫声~");
    }

    public Animal() {
    }

    public Animal(String animalName) {
        this.animalName = animalName;
        System.out.println("你好,我叫"+animalName);
    }
}

3.2.2 新建Dog类

public class Dog extends Animal {
    String type = "犬类动物";

    public Dog() {
        super("小白");
        this.type = type;
    }

    public void printType() {
        System.out.println(type);
        System.out.println(super.type);
    }

}

3.2.3 在测试主类中展示

public class ExtendsTest {
    public static void main(String[] args) {
        String name = "波斯猫";
        Cat cat = new Cat(name);
        cat.shout();
        Dog dog = new Dog();
        dog.printType();

    }
}

结果如下:

可以看到,Dog子类里使用构造方法的时候,运用super关键字成功调用了父类的构造方法——将“小白” 给到了父类的成员变量。

4.实验题目

尽量少写相同的代码编写程序描述类层次,其中 Person 为父类,其属性包括姓 名,性别,出生日期等,方法为 printInfo()打印信息。教师 Tercher 还包括学校和工号属性; 学生 Student 还包括学校、学号、专业、年级和班级等属性;编写一个测试类 TestPerson, 在 main 方法中创建 1 名教师和一名学生对象,输出其属性信息

4.1Person类

public class Person {
    String name;
    char sex;
    String birth;

    void printInfo() {
        System.out.println("姓名:"+name);
        System.out.println("性别:"+sex);
        System.out.println("出生日期:"+birth);

    }

    public Person(String name, char sex, String birth) {
        this.name = name;
        this.sex = sex;
        this.birth = birth;
    }

    public Person() {
    }
}

4.2 Student类

public class Student extends Person{
    String school;
    int studentID;
    String major;
    int grade;
    int class1;

    public Student(String school, int studentID) {
        this.school = school;
        this.studentID = studentID;
    }

    public Student(String school, int studentID, String major, int grade, int class1) {
        this.school = school;
        this.studentID = studentID;
        this.major = major;
        this.grade = grade;
        this.class1 = class1;
    }

    @Override
    void printInfo() {
        super.printInfo();
        System.out.println("主修课程:" + major);
        System.out.println("年级:" + grade);
        System.out.println("班级:"+ class1);
    }
}

4.3Teacher类

class Teacher extends Person{
    String school;
    int teacherID;


    public Teacher(String school, int teacherID) {
        this.school = school;
        this.teacherID = teacherID;

    }
}

4.4 TestPerson类

public class TestPerson {
    public static void main(String[] args) {
        Teacher t1 = new Teacher("****",9527);
        t1.name = "刘德华";
        t1.birth = "1997-12-31";
        t1.sex = '男';
        t1.printInfo();
        Student s1 = new Student("****",9545);
        s1.name = "李通红";
        s1.birth = "2003-05-04";
        s1.sex = '女';
        s1.printInfo();
        Student s2 = new Student("*****",9555,"数学",2,2);
        s2.name = "李通红";
        s2.birth = "2003-04-01";
        s2.sex = '男';
        s2.printInfo();

    }
}

总结

1、通过继承可以更有效的组织程序结构,明确类之间的关系,并充分利用已有的类来开发 新类,以完成更复杂的设计、开发。

2、多态则可以统一多个相关类的对外接口,并在运行时,根据不同的情况,执行不同的操 作,提高类的抽象度和灵活性。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值