11月26日报告

1.继承

(1)super关键字 指的是当前类的父类

父类变量

System.out.println(super.num);

(2)子类在创建对象时,会先完成父类的初始化操作,

调用父类构造器

(3)当父类中只有有参构造方法时,

请在子类中构造方法中手动调用父类有参构造方法

package com.langsin.ExtendsDemo2;

public class daughter2 extends Mother2 {

    public daughter2(){
        super(100);
    }
    public static void main(String[] args) {
    //子类在创建对象时,会先完成父类的初始化操作
        new daughter2();
    }
}

this()是子类构造方法

super()是父类构造方法

要写在构造器第一行

2.方法的重写规则

(1)方法名相同,形参列表相同

(2)子类方法返回值类型要与父类相等或更小

(3)子类权限要比父类权限大或相等

3.多态

父类类型 变量名=new 子类对象;

Animal animal1=new cat;

Animal animal2=new dog;

多态下子类调用方法时,会先检查父类是否有此方法,如果没有,则会报错,

如果有,则会调用子类重写过的方法.

4.选择题

(1)合法标识符: 字母,数字,下划线,美元符号组成,数字不能开头,不能是关键字保留字

(2)012    0开头的八进制

          0x开头的是十六进制

(3)floot f=5+5.5;            不能通过编译,10.5默认为Double类型,结果要加F(f)

(4)强制类型转换

package com.langsin.ex;

public class Test2 {
    public static void main(String[] args) {
        short a,b,C;
        a=1;
        b=2;
        C= (short) (a+b) ;
        a+=2;
        a= (short) (a+2);
        
    }
}

(5)数组

package com.langsin.ex;

public class Test2 {


    public static void main(String[] args) {
        int[] myArray = new int[3];
        myArray[0] = 1;
        myArray[1] = 1;
        myArray[2] = 1;

        myArray = new int[6];
        for (int i = 0; i < myArray.length; i++) {
            System.out.println(myArray[i]);
        }
    }
}

定义一个长度为3的数组,将其长度改为6,

前三个数据丢失,全部都为0

(6)

package com.langsin.ex;

public class MethodDemo {
    public static void f1(byte b) {
        System.out.println("byte");
    }

    public static void f1(short b) {
        System.out.println("short");
    }

    public static void f1(int b) {
        System.out.println("int");
    }

    public static void f1(long b) {
        System.out.println("long");
    }

    public static void f1(char b) {
        System.out.println("char");
    }

    public static void f1(float b) {
        System.out.println("flout");
    }

    public static void f1(double b) {
        System.out.println("double");
    }

    public static void main(String[] args) {
f1(1);
    }
}

f1方法传入1,先执行int,把int注释掉,其次是long,

把long注释掉,执行float,

把float注释掉,执行double,

把double注释掉,报错

需要强制类型转换才能执行byte

再把byte注释掉,执行short

5.引用数据类型的强制类型转换

Animal animal1=new Cat();

Cat cat1=(Cat) animal1;

把父类的引用变量转换为子类引用变量

package com.langsin.polymorphic;

public class Animal {
//父类动物类
public void eat(){
    System.out.println("吃东西..........");
}
}
package com.langsin.polymorphic;

public class Cat extends Animal{

//子类猫类
    @Override
    public void eat() {
        System.out.println("猫猫吃鱼");
    }
    public void catchMouse(){
        System.out.println("抓老鼠");
    }
}
package com.langsin.polymorphic;

public class Test {

//测试类

    public static void main(String[] args) {
      Animal animal1=new Cat();
      Cat cat1=(Cat)animal1;
      cat1.eat();
        cat1.catchMouse();
    }

}

通过把保存Cat对象的Animal类型变量animal1强制转换为Cat类型,

并使用Cat类型变量cat1接收后,

才可以用cat1调用Cat类中catchMouse方法

package com.langsin.polymorphic;

public class Dog extends Animal{

//子类狗类

    @Override
    public void eat() {
        System.out.println("狗狗吃肉");
    }
    public void watchHouse(){
        System.out.println("看家");
    }
}
package com.langsin.polymorphic;

public class Test {

//测试类

    public static void main(String[] args) {
      Animal animal1=new Cat();
      Cat cat1= (Dog) animal1;
      cat1.eat();
        cat1.catchMouse();
    }

}

如果将保存Cat对象animal1转换成Dog类型,

会出现类型转换异常

如果需要判断是否能够强制类型转换,

使用instanceof进行判断

package com.langsin.polymorphic;

public class Test {
    public static void main(String[] args) {
     Animal animal1=new Dog();
     if (animal1 instanceof Cat){
         Cat cat1=(Cat) animal1;
         animal1.eat();
     }
     if (animal1 instanceof Dog){
     Dog dog1=(Dog) animal1;
     animal1.eat();
     ((Dog) animal1).watchHouse();
     };
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蒂法挤挤挤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值