Java 入门25 继承

2022/6/25 14:23:38

本文主要是介绍Java 入门25 继承,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

 

 

 

 

 

 Person类

package com.ITheima.d7_extends;

public class Person {
    private String name;
    private  int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
View Code

Student类继承Person类

package com.ITheima.d7_extends;

public class Student extends Person{
    public  void study(){
        System.out.println(getName()+"好好学习");
    }
}
View Code

Teacher类继承Person类

package com.ITheima.d7_extends;

public class Teacher extends Person {
    public void teach() {
        System.out.println(getName()+"开始教导学生");
    }
}
View Code

 

案例

 



这篇关于Java 入门25 继承的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程