Java基础 5.2

1.继承本质的详解

package com.logic.extend_;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

public class ExtendsTheory {
    public static void main(String[] args) {
        Son son = new Son();//内存的布局
        /*
        按照查找关系来返回信息
        1.首先看子类是否有该属性
        2.如果子类有这个属性 并且可以访问 则返回信息
        3.如果子类没有这个属性 就看父类有没有这个属性(如果父类有该属性 并且可以访问 就返回信息..)
        4.如果父类没有找到 就按照(3)的规则 继续找上级父类 知道Object...
         */
        System.out.println(son.name);
        System.out.println(son.hobby);
    }
}

class GrandPa {
    String name = "大头爷爷";
    String hobby = "旅游";

    public GrandPa() {
        System.out.println("GrandPa()被调用~~");
    }
}
class Father extends GrandPa {
    String name = "大头爸爸";
    private int age = 30;

    public Father() {
        System.out.println("Father()被调用~~");
    }
}
class Son extends Father {
    String name = "大头儿子";

    public Son() {
        System.out.println("Son()被调用~~");
    }
}

2.继承的练习

分析输出结果

package com.logic.extend_;

public class ExtendsPractice {
    public static void main(String[] args) {
        B b = new B();
    }
}

class A {
    public A() {
        System.out.println("a");
    }
}

class B extends A {
    public B() {
        this("abc");
        System.out.println("b");
    }

    public B(String name) {
        //默认有一个super
        System.out.println("b name");
    }
}

  

package com.logic.extend_;

import com.logic.modifier.B;

public class ExtendsPractice {
    public static void main(String[] args) {
        C c = new C();
    }
}

class A {
    public A() {
        System.out.println("我是A类");
    }
}

class B2 extends A {
    public B2() {
        System.out.println("我是B2类的无参构造器");
    }

    public B2(String name) {
        System.out.println("我是B2类的有参构造器");
    }
}

class C extends B2 {
    public C() {
        this("hello");
        System.out.println("我是C类的无参构造器");
    }

    public C(String name) {
        super("hahah");
        System.out.println("我是C类的有参构造器");
    }

}

 

编程代码

package com.logic.extend_;

import sun.plugin2.gluegen.runtime.CPU;

/*
1.编写Computer类 包含CPU 内存 硬盘等属性 getDetails方法用于返回
Computer的详细信息
2.编写PC子类 继承Computer类 添加特有属性 brand
3.编写Notepad子类 继承Computer类 添加特有属性 color
4.编写Test类 在main方法中创建PC和NotePad对象 分别给对象中特有的属性赋值
以及从Computer类继承的属性赋值 并使用方法并打印输出信息
 */
public class ExtendsPractice {
    public static void main(String[] args) {//主方法测试
        PC pc = new PC("R7-7840HS", "32G", "1T", "Lenovo");
        pc.getInfo();
        NotePad notePad = new NotePad("R9-9800X3D", "64G", "2T", "ROG");
        notePad.getInfo();
    }
}

class Computer {//编写父类相关信息
    private String CPU;
    private String internal_storage;
    private String hard_disk;

    public Computer(String CPU, String internal_storage, String hard_disk) {//父类的有参构造器
        this.CPU = CPU;
        this.internal_storage = internal_storage;
        this.hard_disk = hard_disk;
    }

    public String getCPU() {
        return CPU;
    }

    public void setCPU(String CPU) {
        this.CPU = CPU;
    }

    public String getInternal_storage() {
        return internal_storage;
    }

    public void setInternal_storage(String internal_storage) {
        this.internal_storage = internal_storage;
    }

    public String getHard_disk() {
        return hard_disk;
    }

    public void setHard_disk(String hard_disk) {
        this.hard_disk = hard_disk;
    }

    public String getDetailsInfo() {//输出父类的所有信息
        return "CPU: " + CPU + " internal_storage: " +
                internal_storage + " hard_disk: " + hard_disk;
    }
}

class PC extends Computer {//继承父类的相关属性
    private String brand;

    public PC(String CPU, String internal_storage, String hard_disk, String brand) {//子类的有参构造器
        super(CPU, internal_storage, hard_disk);//指定父类的有参构造器
        this.brand = brand;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public void getInfo() {
        System.out.println(getDetailsInfo() + " brand: " + brand);//合并输出父类信息和子类信息
    }
}

class NotePad extends Computer {
    private String color;

    public NotePad(String CPU, String internal_storage, String hard_disk, String color) {//子类的有参构造器
        super(CPU, internal_storage, hard_disk);//指定父类的有参构造器
        this.color = color;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public void getInfo() {//合并输出父类信息和子类信息
        System.out.println(getDetailsInfo() + " color: " + color);
    }
}

3.super关键字

基本介绍

super代表父类的引用 用于访问父类的属性、方法、构造器

基本语法

  • 访问父类的属性 但不能访问父类的private属性 super.属性名
  • 访问父类的方法 但不能访问父类的private方法 super.方法名(参数列表)
  • 访问父类的构造器 super(参数列表)只能放在构造器的第一句 只能出现一句! 
package com.logic.super_;

public class A {
    public int n1 = 100;
    protected int n2 = 200;
    int n3 = 300;
    private int n4 = 400;

    public A() {
    }

    public A(int n1) {
        this.n1 = n1;
    }

    public void test100() {

    }

    protected void test200() {

    }

    void test300() {

    }

    private void test400() {

    }
}
package com.logic.super_;

public class B extends A {
    //访问父类的属性 但不能访问父类的private属性 super.属性名
    public void hi() {
        System.out.println(super.n1 + " " + super.n2 + " " + super.n3);
    }
    public void hello() {
        super.test100();
        super.test200();
        super.test300();
    }
    //访问父类的构造器 super(参数列表)只能放在构造器的第一句 只能出现一句
    public B() {
        super();
    }

}

super使用细节

  • 调用父类的构造器的好处(分工明确 父类属性由父类初始化 子类的属性由子类初始化) 
  • 当子类中有和父类中的成员(属性和方法)重名时 为了访问父类的成员 必须通过super 如果没有重名 使用super、this、直接访问是一样的效果(this 和 默认查找规则等价 都是从本类开始查找 super则是跳过本类父类开始查找)
  • super的访问不限于直接父类 如果爷爷类和本类有同名的成员 也可以使用super去访问爷爷类的成员 如果多个基类中都有同名的成员 使用super访问遵循就近原则

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值