Java学习_Day 08(学习内容:狂神说JAVA零基础3P64-3P72)

3P64 构造器详解

package com.oop;

public class P64_person {
    // 一个类什么都不写,也会默认存在一个构造器
    // 构造方法不能有返回类型,且必须和类名一致
    // 显示定义构造方法
    String name;
    // 实例化初始值
    // 1.使用new关键字,本质是在调用构造器
    // 2.构造器用来初始化值
    public P64_person(){
        // this.name = "Rye";
    }
    // 有参构造
    // 一旦定义了有参构造,无参就必须显式定义
    public P64_person(String name){
        this.name = name;
    }

    // alt + insert 生成构造器

}
package com.oop;

public class P64 {
    public static void main(String[] args) {
        // 使用new关键字实例化一个对象
        P64_person p64_person = new P64_person("Rye");

        System.out.println(p64_person.name);
    }
}

/*
构造器:
    1. 和类名相同
    2. 没有返回值
作用:
    1. new本质是在调用构造方法
    2. 初始化对象值
注意点:
    1. 定义有参构造之后,必须显式定义无参构造
    2. alt +insert
    3. this
 */

3P65 创建对象内存分析

在这里插入图片描述

package com.oop;

public class P65 {
    public static void main(String[] args) {
        P65_pet dog = new P65_pet();
        dog.name = "旺财";
        dog.age = 3;
        dog.shout();

        System.out.println(dog.name);
        System.out.println(dog.age);
    }
}
package com.oop;

public class P65_pet {
    public String name;
    public int age;

    public void shout(){
        System.out.println("叫了一声");
    }
}

3P66 简单小结类与对象

package com.oop;

public class P66 {
    public static void main(String[] args) {
        /*
        1. 类与对象
        类是一个模板:抽象。对象是一个具体的实例
        2. 方法
        定义和调用
        3. 对象的引用
        引用类型/基本类型(八个)
        对象是通过引用来操作的  栈-->堆(地址)
        4. 对象属性/字段/成员变量
        默认初始化:数字、char、布尔、引用类型(null)
        修饰符 属性类型 属性名 = 属性值;
        5. 对象的创建和使用
        - 必须使用new关键字创建对象,必须有构造器 Person rye = new Person()
        - 对象的属性  rye.name
        - 对象的方法  rye.sleep()
        6. 类:
        静态的属性和动态的行为
        
        封装、继承与多态
         */
    }
}

3P67 封装详解

package com.oop;
/*
    1. 提高程序安全性,保护数据
    2. 隐藏代码的实现细节
    3. 统一接口
    4. 系统可维护性增加了
 */
public class P67 {
    public static void main(String[] args) {
        P67_student s1 = new P67_student();
        //String name = s1.getName();
        s1.setName("Rye");
        System.out.println(s1.getName());

        s1.setAge(70); // 不合法的
        System.out.println(s1.getAge());
    }
}
package com.oop;
// 类 private 私有
public class P67_student {
    // 主要是为了封装属性
    // 属性私有
    private String name;    // 名字
    private int id;    // 学号
    private char sex;    // 性别
    private int age;
    // 提供一些可以操作属性的方法
    // 提供一些pubilc的get、set方法
    // get 读
    // set 写
    public String getName(){
        return this.name;
    }

    public void setName(String name){
        this.name = name;
    }
    // alt + insert

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age > 120 || age < 0){
            this.age = 3;
        }else {
            this.age = age;
        }
    }
}

3P68 继承详解

package com.oop;

public class P68 {
    public static void main(String[] args) {
        // 子类继承父类,就会拥有父类的全部方法
//        P68_student Rye = new P68_student();
//        Rye.say();
//        System.out.println(Rye.money);
        P68_person p68_person = new P68_person();
    }
}
package com.oop;
// person 父类  所有类都默认继承object类
public class P68_person {
    // public\protected\default\private
//    private int money = 10_0000_0000;
//    public void say(){
//        System.out.println("说话");
//    }
//
//    public int getMoney() {
//        return money;
//    }
//
//    public void setMoney(int money) {
//        this.money = money;
//    }
//    Object
}
package com.oop;
// student extends person 子类/派生类
public class P68_student extends P68_person{
    // Ctrl + H 继承树
}
package com.oop;
// teacher extends person 子类/派生类
public class P68_teacher extends P68_person{
}

3P69 super详解

package com.oop;

public class P69 {
    public static void main(String[] args) {
        P69_student p69_student = new P69_student();
//        p69_student.test("JJ");
//        p69_student.test1();
    }
}
/*
super注意点:
    1. super调用父类的构造方法,必须在构造方法的第一个
    2. super必须只能出现在子类的方法或者构造方法中
    3. super和this不能同时调用构造方法
this注意点:
    1. 代表的对象不同
    2. 前提不同 this在没有继承时也可以使用 super只能在继承条件下才能使用
    3. 构造方法 this()本类构造 super()父类构造
 */
package com.oop;

public class P69_person {

    public P69_person() {
        // 隐藏代码 调用了父类的无参构造
        super(); //调用父类构造器必须在子类构造器第一行
        System.out.println("Person无参构造执行了");
    }

    protected String name = "Rye";
    // private无法被继承
    public void print(){
        System.out.println("Person");
    }
}
package com.oop;

public class P69_student extends P69_person {
    public P69_student() {
        System.out.println("子类无参构造执行了");
    }

    private String name = "Antares";
    public void test(String name){
        System.out.println(name);
        System.out.println(this.name);
        System.out.println(super.name);
    }
    public void print(){
        System.out.println("Student");
    }
    public void test1(){
        print();
        this.print();
        super.print();
    }
}

3P70 方法重写

package com.oop;

public class P70 {
    // 静态方法不能被重写
    public static void main(String[] args) {
        // 非静态:重写  重写关键字只能是public
        // 静态方法:方法的调用只和定义左边的数据类型有关
        P70_A a = new P70_A();
        a.test();
        // 父类的引用指向子类  A重写了B类的方法
        P70_B b = new P70_A(); // 子类重写了父类的方法,重写只和非静态有关
        b.test();
    }
}
/*
静态方法是类的方法,非静态方法是对象的方法
P70_A a = new P70_A()是调用了A的构造方法new出了一个A对象,引用了A类型
P70_B b = new P70_A()是调用了A的构造方法new出了一个A对象,引用了B类型
b.test()在有static时调用类方法,无static时调用对象方法
 */
/*
重写: 需要有继承关系,子类重写父类的方法
    1. 方法名必须相同
    2. 参数列表必须相同
    3. 修饰符范围可以扩大,但不能缩小 public>protected>default>private
    4. 抛出的异常:范围可以被缩小,但不能扩大  ClassNotFoundException --> Exception(大)

重写,子类的方法和父类必须一致,方法体不同。

为什么需要重写:
    1. 父类功能,子类不一定需要,或者不一定满足
    Alt + insert:override
 */
package com.oop;

public class P70_A extends P70_B{
//    public void test(){
//        System.out.println("A=>test");
//    }

    // 重写
    @Override //注解
    public void test() {
        System.out.println("A=>test");
    }
}
package com.oop;
// 重写都是方法的重写,和属性无关
public class P70_B {
    public void test(){
        System.out.println("B=>test");
    }
}

3P71 多态详解

package com.oop;

public class P71 {
    public static void main(String[] args) {
        // 一个对象的实际类型是确定的
        // new P71_student();
        // 可以指向的引用类型并不确定
        // Student能调用的方法都是自己的或者继承父类的
        P71_student s1 = new P71_student();
        // 父类的引用指向子类的类型
        // 向上转型
        // Person父类可以指向子类,但不能调用子类独有的方法
        P71_person s2 = new P71_student();
        Object s3 = new P71_student();
        s2.run(); // 子类重写了父类的方法,就执行子类的方法
        s1.run();
        s1.eat();
        // 对象能执行哪些方法,主要看对象左边的类型,和右边关系不大
        // s2.eat(); 父类没有eat方法
        // 子类重写了父类方法就调用子类的,没有重写就调用父类的
    }
}
/*
多态注意事项:
    1.多态是方法的多态,属性没有多态
    2.父类和子类有联系   类型转换异常ClassCastException
    3.存在条件: 继承关系,方法需要重写,父类引用 指向 子类对象
不能被重写,无法具有多态:
    1. static 方法属于类,不属于实例
    2. final 常量
    3. private方法
 */
package com.oop;

public class P71_person {
    public void run(){
        System.out.println("run");
    }
}
package com.oop;

public class P71_student extends P71_person{
    @Override
    public void run() {
        System.out.println("son");
    }

    public void eat(){
        System.out.println("eat");
    }
}

3P72 instanceof和类型转换

package com.oop;

public class P72 {
    public static void main(String[] args) {
//        // Object > String
//        // Object > Person > teacher
//        // Object > Person > Student
//        Object object = new P72_student();
//        // instanceof判断栈里的类名指向的对象是否是指定类型或者是其子类
//        System.out.println(object instanceof P72_student);
//        System.out.println(object instanceof P72_person);
//        System.out.println(object instanceof Object);
//        System.out.println(object instanceof P72_teacher);
//        System.out.println(object instanceof String);
//
//        P72_person person = new P72_student();
//        System.out.println(person instanceof P72_student);
//        System.out.println(person instanceof P72_person);
//        System.out.println(person instanceof Object);
//        System.out.println(person instanceof P72_teacher);
        System.out.println(person instanceof String); 编译报错
//
//        P72_student student = new P72_student();
//        System.out.println(student instanceof P72_student);
//        System.out.println(student instanceof P72_person);
//        System.out.println(student instanceof Object);
        System.out.println(student instanceof P72_teacher);
        // 类型之间的转换
        // 低转高无需强制转换
//        P72_person obj = new P72_student();
//        // student将这个对象强制转换为student类型,就可使用student类型方法
//        // 目的是为了不重写 父类就能调用子类方法
//        P72_student student = (P72_student) obj;
//        student.go();
        P72_student student = new P72_student();
        student.go();
        // 子类转换为父类可能丢失一些方法
        P72_person person = student;

    }
}
/*
1. 父类引用指向子类对象
2. 子类转换为父类--向上转型 不用强制转换
3. 父类转换为子类--向下转换 需要强制转换 会丢失方法
4. 转换方便方法调用,减少重复代码

抽象:封装、继承和多态!   抽象类、接口
 */
package com.oop;

public class P72_person {
    public void run(){
        System.out.println("run");
    }
}
package com.oop;

public class P72_student extends P72_person{
    public void go(){
        System.out.println("go");
    }
}
package com.oop;

public class P72_teacher extends P72_person{
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值