面向对象三大特征

一.封装

实现步骤:

1.将属性私有化private

2.提供一个get/set方法

3.set用于对属性赋值,get用于获取属性的值

权限修饰符

private < 缺省  <protected < public ,访问控制级别越来越大

package Arrays;

public class Test {
    public static void main(String[] args) {
        Circle circle = new Circle();
        circle.setRadous(1);
        double area = circle.Area();
        System.out.println(area);
    }
}
class Circle{
    private int radous;

    public int getRadous() {
        return radous;
    }

    public void setRadous(int radous) {
        this.radous = radous;
    }
    public double Area(){
        return Math.PI * radous * radous;
    }
}

二.继承

Java里的继承是通过extends来实现的

其中需要注意一些细节:

1.Java只支持单继承,不支持多继承

2.子类会继承父类所有的属性和方法,且无论是public还是private,但是子类无法对父类的私有成员直接进行访问,需要通过继承的公有方法(get/set)来访问

3.子类必须调用父类的构造器

   (1)默认情况下,子类都会调用父类的空参构造器

   (2)如果父类没有空参构造器,子类必须手动编写构造器,且子类构造器首行必须通过                 this(参数列表)或者super(参数列表)语句指定调用本类或者父类中相应的构造                 器,否则报错

4.不能滥用继承,子类和父类一定要满足 is-a的关系

下面代码来解释

子类都会调用父类的空参构造器

package Arrays;

public class Person {
    private String name;

    public Person() {
        System.out.println("父类的无参构造器");
    }

    public Person(String name) {
        this.name = name;
        System.out.println("父类的有参构造器");
    }

    public String getName() {
        return name;
    }

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

public class Student extends Person{
    private int score;

    public Student() {
        System.out.println("子类无参构造器");
    }



    public Student(String name, int score) {
        super(name);
        this.score = score;
        System.out.println("子类有参构造器");
    }
}
package Arrays;

public class Test {
    public static void main(String[] args) {
        Student student1 = new Student();
        System.out.println();
        Student student2 = new Student("jack",100);

    }
}

 其他细节可以自己实现一下

三.多态

1.一个对象的编译类型和运行类型可以不一致

2.编译类型在定义对象时就已经确定好了

3.运行类型是可以变化的

4.编译类型看=号左边,运行类型看=号右边

package Method;

public class PolyDetails {
    public static void main(String[] args) {
        Animal animal = new Cat();
        animal.eat();

    }
}
class Animal{
    public void eat(){
        System.out.println("吃");
    }
    public void sleep(){
        System.out.println("睡");
    }
}
class Cat extends Animal{
    public void eat(){
        System.out.println("猫吃鱼");
    }
    public void skill(){
        System.out.println("猫会爬树");
    }
}

向上转型和向下转型

package Method;

public class PolyDetails {
    public static void main(String[] args) {
        Animal animal = new Cat();//向上转型
        animal.eat();

        Cat cat = (Cat) animal;//向下转型
        cat.skill();

    }
}
class Animal{
    public void eat(){
        System.out.println("吃");
    }
    public void sleep(){
        System.out.println("睡");
    }
}
class Cat extends Animal{
    public void eat(){
        System.out.println("猫吃鱼");
    }
    public void skill(){
        System.out.println("猫会爬树");
    }
}

属性不能重写

属性没有重写之说,属性的值只看编译类型

package Method;

public class PolyDetails {
    public static void main(String[] args) {
        Animal animal = new Cat();
       
        System.out.println(animal.a);//发现a的值依旧是10
    }
}
class Animal{
    int a =10;
   
}
class Cat extends Animal{
    int a =20;
  
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值