Java学习Day4——继承,类型,多态,封装,JavaBean,接口

109 阅读4分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第4天,点击查看活动详情

1.继承

代码Demo:

public class TestInheritance {
    public static void main(String[] args) {
        B b = new B();
        System.out.println(b.name);
    }

}
class A{
    String name;
    void test(){
        System.out.println("这是父类");
    }
}

class B extends A{

}

子类可以直接使用父类的字段和对象方法,但是不能使用父类的构造方法

此时就会报错

image.png

原因:

1.构造方法不能被继承

2.对于子类来说,父类中有带参构造,子类中也应该有相应的给name赋值

2.子类调用父类构造方法

给子类加一个自己的构造方法,然后在子类的构造方法中调用父类的构造方法

public class TestInheritance {
    public static void main(String[] args) {
        B b = new B("张三");
        System.out.println(b.name);
    }

}
class A{
    String name;


    public A(String name){
        this.name = name;
    }
    void test(){
        System.out.println("这是父类");
    }

}

class B extends A{
    B(String name){
        super(name);
    }
}

2.类型种类

1.基本类型:八大基本类型

2.引用类型:重点null

3.包装类型:与基本类型相对应,有对象的各种特征(可以实例化,有字段,有方法,可继承等)

当引用类型为null时,使用时会出现NullPointerException

避免:使用前加上判断

3.类型转换

1.基本类型间转换规则

Boolean类型不能转换

image.png

顺箭头->隐式转化 逆箭头->强制转化

注意:范围之内强制转换不会丢失精度,范围之外会损失精度

代码Demo:

public class TestTypeCast {
    public static void main(String[] args) {
        int a=1000;
        byte b = (byte) a;
        System.out.println(b);
    }
}

输出结果:-24

结果分析:

byte精度为128,128*8=1024 1000-1024=-24

2.包装类型和基本类型间转换规则

image.png

都是隐式转换

3.引用类型之间的转换规则

image.png

顺箭头->隐式转化(待转换对象和目标对象要符合【is a】关系) 向上转型

逆箭头->强制转化(待转换对象和目标对象要符合【is a】关系) 向下转型

隐式转化:

public class TestTypeCast {
    public static void main(String[] args) {

        Animal a = new Cat();//实质还是cat类型
        
    }
}

class Animal extends Object{}
class Cat extends Animal{}

强制转化:

public class TestTypeCast {
    public static void main(String[] args) {

        Animal a = new Cat();
        Cat c = (Cat) a;

    }
}

class Animal extends Object{}
class Cat extends Animal{}

类型判断

1.getClass()方法

2.instanceof()方法 :判断检查对象和类型之间是否属于【is a】关系

4.不属于上述三种的其他类型转化,借助转换方法

代码Demo:

public class TestTypeCast {
    public static void main(String[] args) {

//        Animal a = new Cat();
//        Cat c = (Cat) a;
        String a="1";
        String b="2";
        System.out.println(Integer.parseInt(a) + Integer.parseInt(b));

4.多态

如果同一个方法执行时,会表现出不同行为,称此方法具备多态性

前提条件: 1.用父类型代表子类对象

2.分类和子类有相同方法(方法重写,Override)

IDEA快捷键:ctrl+O

总结

image.png

5.封装

访问修饰符:

image.png

JavaBean

JavaBean规范

1.字段私有,public方法访问私有字段 get set is

2.最好提供一个无参构造

3.最好实现一个借口类 Serializable

代码Demo:

class Teacher implements Serializable {
    private String name;
    private boolean married;
   public String getName(){
       return this.name;
   }
    public void setName(String name){
        this.name = name;
    }

    public boolean isMarried(){
       return this.married;
    }
    public void setMarried(boolean married){
       this.married = married;
    }
    public Teacher(){
       
    }
}

注意:boolean类型要用is命名

IDEA自动生成:

在空白处右键点击生成

image.png

image.png

修改访问私有属性:

public class TestJavaBean {
    public static void main(String[] args) {
        Teacher teacher = new Teacher("张老师",false);
        System.out.println(teacher.getName());
        teacher.setMarried(true);
        System.out.println(teacher.isMarried());
        
    }

}
class Teacher implements Serializable {
    private String name;
    private boolean married;
    private int age;
   public String getName(){
       return this.name;
   }
    public void setName(String name){
        this.name = name;
    }

    public boolean isMarried(){
       return this.married;
    }
    public void setMarried(boolean married){
       this.married = married;
    }
    public Teacher(String name,boolean married){
       this.name = name;
       this.married = married;

    }
//    public Teacher(){
//
//    }

    public int getAge() {
        return age;
    }

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

6.接口

1.解决单继承

需要放入接口的方法,必须加default关键字(默认方法)

default方法只能是public

代码Demo:

public class TestInterface {
    public static void main(String[] args) {
        Duck duck = new Duck();
        duck.fly();
        duck.swim();
    }
}

    interface Swimmable{
        public default void swim(){
            System.out.println("游泳");

        }
    }
    interface Flyable{
        public default void fly(){
            System.out.println("飞翔");
        }
    }
class Duck implements Swimmable,Flyable{

}

2.多态

前提条件:

1.用父类型代表子类对象,后者用接口类型代表实现类对象

2.分类和子类有相同方法(方法重写,Override)

代码Demo(默认方法):

public class TestInterface1 {
    public static void main(String[] args) {
        E[] arr = new E[]{
                new F(),
                new G()
        };
        for (int i = 0; i < arr.length; i++) {
            E e = arr[i];
            e.e();
        }

    }
}
interface E{
    default void e(){
        System.out.println("e");
    }

}
class F implements E{
    @Override
    public void e(){
        System.out.println("f");
    }
}

class G implements E{
    @Override
   public void e(){
        System.out.println("g");
    }
}

抽象方法:

1.只能是public的,public abstr可以省略

2.没有多态时使用默认方法,有多态时使用抽象方法

代码Demo(抽象方法):

public class TestInterface1 {
    public static void main(String[] args) {
        E[] arr = new E[]{
                new F(),
                new G()
        };
        for (int i = 0; i < arr.length; i++) {
            E e = arr[i];
            e.e();
        }

    }
}
interface E{
    //默认方法
//    default void e(){
//        System.out.println("e");
//    }
    //抽象方法
    public abstract void e();
}
class F implements E{
    @Override
    public void e(){
        System.out.println("f");
    }
}

class G implements E{
    @Override
   public void e(){
        System.out.println("g");
    }
}

3.封装

只能用接口中的方法,对实现类中的方法和字段都调用不了,从而实现类对外隐藏细节

代码Demo:

image.png