自学内容网 自学内容网

Java(面向对象进阶-继承)

 什么是继承

 

 

 继承的特点

 

 

 

 

 继承练习

 

 

package day9.继承;

public class animals {
    public static void eat(){
        System.out.println("吃饭");
    }
    public static void drink(){
        System.out.println("喝水");
    }
}
package day9.继承;

public class cat extends animals {
    public static void f(){
        System.out.println("抓老鼠");
    }
}
package day9.继承;

public class dog extends animals {
    public static void f(){
        System.out.println("看家");
    }
}
package day9.继承;

public class lihua_cat extends cat {
    public static void f(){
        System.out.println("我是狸花猫");
    }
}
package day9.继承;

public class buou_cat extends cat {
    public static void f(){
        System.out.println("我是布偶猫");
    }
}
package day9.继承;

public class hashiqi_dog {
    public static void f(){
        System.out.println("我是哈士奇,我要拆家");
    }
}
package day9.继承;

public class taidi_dog extends dog {
    public static void f(){
        System.out.println("我是泰迪,我要蹭蹭");
    }
}


 

package day9.继承;

public class test {
    public static void main(String[] args) {
        buou_cat f1=new buou_cat();
        f1.f();
        lihua_cat f2=new lihua_cat();
        f2.f();
        hashiqi_dog f3=new hashiqi_dog();
        f3.f();
        taidi_dog f4=new taidi_dog();
        f4.f();
    }
}

 

子类可以继承父类中的什么 

 构造方法

父类的构造方法不能被子类继承

成员变量

父类的成员变量可以被继承,非私有可以使用,私有的不能使用 

成员方法

 

 只有父类中的虚方法才能被子类继承

访问特点

成员变量的访问特点

package day9;

public class test1 {
    public static void main(String[] args) {
        zi z=new zi();
        z.show();
    }
}
class fu{
    String name="父亲";
    String hobby="喝茶";
}
class zi extends fu{
    String name="儿子";
    String hobby="打代码";
    public void show(){
        System.out.println(name);
        System.out.println(hobby);
        System.out.println(this.name);
        System.out.println(this.hobby);
        System.out.println("----------------");
        System.out.println(super.name);
        System.out.println(super.hobby);
    }
}

 

成员方法的访问特点

 

 

 

 练习

 

package day9;
/*练习 利用方法的重写设计继承结构
现在有三种动物:哈士奇,沙皮狗,中华田园犬
暂时不考虑属性,只要考虑行为。
请按照继承的思想特点进行继承体系的设计。
三种动物分别有以下的行为:
哈士奇: 吃饭(吃狗粮) 喝水, 看家, 拆家
沙皮狗 吃饭(吃狗粮,吃骨头) 喝水, 看家
中华田园犬: 吃饭(吃剩饭) 喝水, 看家*/
public class test2 {
    public static void main(String[] args) {
        System.out.print("哈士奇");
        hashiqi.eat();
        hashiqi.drink();
        hashiqi.kanjia();
        hashiqi.chaijia();
        System.out.println("------------------");
        System.out.print("沙皮狗");
        shapi.eat();
        shapi.drink();
        shapi.kanjia();
        System.out.println("------------------");
        System.out.print("中华田园犬");
        tianyuan.eat();
        tianyuan.drink();
        tianyuan.kanjia();
    }
}
class dog{
    public static void eat(){
        System.out.println("吃饭");
    }
    public static void drink(){
        System.out.println("喝水");
    }
    public static void kanjia(){
        System.out.println("看家");
    }
}
//哈士奇: 吃饭(吃狗粮) 喝水, 看家, 拆家
class hashiqi extends dog{
    public static void eat(){
        System.out.println("吃狗粮");
    }
    public static void chaijia(){
        System.out.println("拆家");
    }
}
//沙皮狗 吃饭(吃狗粮,吃骨头) 喝水, 看家
class shapi extends dog{
    public static void eat(){
        System.out.println("吃狗粮,吃骨头");
    }
}
//中华田园犬: 吃饭(吃剩饭) 喝水, 看家
class  tianyuan extends  dog{
    public static void eat(){
        System.out.println("吃剩饭");
    }
}

 

 

构造方法的访问特点

 

 

 

 this、super关键字

练习 

package day9.test3;

public class people {
    public     int id;
    public     String name;
    public double gongzi;
    public  people(int id,String name,double gongzi){
        this.id=id;
        this.name=name;
        this.gongzi=gongzi;
    }
    public  void eat(){
        System.out.println("吃米饭");
    }
    public  void job(){

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public double getGongzi() {
        return gongzi;
    }

    public void setGongzi(double gongzi) {
        this.gongzi = gongzi;
    }
}
package day9.test3;

public class jingli extends people{
    private double jiangjin=1000;

    public jingli(int id, String name, double gongzi) {
        super( id, name, gongzi);
        this.id=id;
        this.name=name;
        this.gongzi=gongzi+jiangjin;
    }

    @Override
    public void job() {
        //super.job();
        System.out.println("管理其他人");
    }

}
package day9.test3;

public class chushi extends people {

    public  chushi(int id,String name,double gongzi){
        super( id, name, gongzi);
        this.id=id;
        this.name=name;
        this.gongzi=gongzi;
    }
    @Override
    public void job() {
        System.out.println("炒饭");
    }
}
package day9.test3;

public class test {
    public static void main(String[] args) {
        jingli j1=new jingli(88,"辛经理",3000);
        chushi c1=new chushi(76,"张大厨",2000);
        System.out.println("id"+j1.getId()+j1.getName()+"工资是"+j1.getGongzi());
        System.out.println("id"+c1.getId()+c1.getName()+"工资是"+c1.getGongzi());
    }
}

 

 

package day9.test4;

public class Employee {
    private int id;
    private String name;
    public Employee(){

    }
    public Employee(int id,String name){
        this.id=id;
        this.name=name;
    }
    public void work(){

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
class Teacher extends Employee{
    public Teacher(int id,String name){
        super(id,name);
    }
    public void f(){
        System.out.println("教研部");
    }
}
class AdminStaff extends Employee{
    public AdminStaff(int id,String name){
        super(id,name);
    }
    public void f(){
        System.out.println("行政部");
    }
}
class Tutor extends Teacher{
    public Tutor(int id,String name){
        super(id,name);
    }
    @Override
    public void work() {
       System.out.println("助教");
    }
}
class Lecturer extends Teacher{
    public Lecturer(int id,String name){
        super(id,name);
    }

    @Override
    public void work() {
        System.out.println("讲师");
    }
}
class Maintainer extends AdminStaff{
    public Maintainer(int id,String name){
        super(id,name);
    }

    @Override
    public void work() {
        System.out.println("维护专员");
    }
}
class Buyer extends AdminStaff{
    public Buyer(int id,String name){
        super(id,name);
    }

    @Override
    public void work() {
        System.out.println("采购专员");
    }
}
package day9.test4;

public class test {
    public static void main(String[] args) {
        Tutor p1=new Tutor(10,"张三");
        System.out.print("id"+p1.getId()+"姓名"+p1.getName()+"职业");
        p1.work();
        System.out.print("属于");
        p1.f();
        System.out.println("---------------------");
        Lecturer p2=new Lecturer(12,"李四");
        System.out.print("id"+p2.getId()+"姓名"+p2.getName()+"职业");
        p2.work();
        System.out.print("属于");
        p2.f();
        System.out.println("---------------------");
        Maintainer p3=new Maintainer(18,"王五");
        System.out.print("id"+p3.getId()+"姓名"+p3.getName()+"职业");
        p3.work();
        System.out.print("属于");
        p3.f();
        System.out.println("---------------------");
        Buyer p4=new Buyer(26,"小六子");
        System.out.print("id"+p4.getId()+"姓名"+p4.getName()+"职业");
        p4.work();
        System.out.print("属于");
        p4.f();
        System.out.println("---------------------");
    }
}

 

 

 


原文地址:https://blog.csdn.net/weixin_68931360/article/details/145189603

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!