面向对象的三大特性

面向对象三大特性:封装、继承和多态。

程序追求的是“高内聚、低耦合”。高内聚:类的内部数据操作细节自己完成,不允许外部干涉; 低耦合:仅暴露小量的方法给外部使用。

------------------------------------------------------------------------------------------------------

封装:可以看作是数据的隐藏;即属性私有,一般通过操作接口来访问,禁止直接访问一个对象中的数据的实际表示。有get或set

例:有两个Java类:

public class Student{

  public static void main(String[] args){

    private String name;

    public String getName(){

      return this.name;

    }

    public void setName(String name){

      this.name=name;

    }

  }

}

//把获取姓名和设置姓名设置成一个个接口,让其它类来调用接口就行,而不显示具体实现细节

public class Apllication{

  public static void main(String[] args){

    Student student =new Student();

    student.setName("BBQ");                //set保存,get获取

    System.out.println(student.getName());

  }

}

 

封装的好处:

1、提高程序的安全性、保护数据

2、隐藏代码的实现细节

3、统一接口

4、系统可维护增加

 

-----------------------------------------------------------------------------------------------------------------

继承:

1、 本质是对某一批类的抽象,从而实现现实世界更好的建模。

2、extands 的意思是“扩展”,子类是父类的扩展。

3、Java类只有单继承,没有多继承。(即一个类只能继承一个父类)

4、继承是类和类的之间的一种关系。

5、继承关系的两个类,一个是子类(派生类),另一个是父类(基类),子类继承父类,使用关键字extends来表示。

6、子类和父类之间,从意义上讲应该具有“is a”的关系。

 

其它关于方法的重写与继承可跳转  https://i-beta.cnblogs.com/posts/edit;postId=12000357

关于super的用法和this的区别可跳转 :https://www.cnblogs.com/bbq668/p/12002740.html

 

----------------------------------------------------------------------------------

 

多态的定义:

即同一种方法可以根据发送对象的不同而采用多种不同的行为方式;

一个对象的实际类型是确定的,但是可以指向对象的引用类型有哼多(父类或有关系的类)

 

多态存在条件:

1、有继承关系;

2、子类重写父类方法;

3、父类引用指向子类的对象;

注意多态是方法的多态,属性没有多态;

 

例子:

public class Preson{

          public void run(){

    System.out.println("This is run1");

  }

  public void BBQ(){

    System.out.println("This is BBQ");

  }

}

 

public class Student extend Preson{

  public void run(){

    System.out.println("This is run2"); 

  }

       public void eat(){

    System.out.println("This is eat")

  }

}

 

public class Application{

  public static void main(String[] args){

          //多态:一个对象的实际类是确定的(左边代码),但是可以有多种指向的引用类型(即等号右边代码)

    Student s1=new Student();

    Preson s2=new Student(); 

    Object  s3=new Studenct();

 

   //多态思想创建出来的对象的使用  

    s1.run();              //输出结果:This is run2      

    s1.eat();    //输出结果:This is eat

    s2.run();    //输出结果:This is run2  ,子类重写了方法run ,所以父类对象执行的是重写后的方法

    s2.BBQ();   //输出结果:This is BBQ          

    s2.eat();   //代码错误,父类不能调用子类新增的方法

    ((Student)s2).eat();  //输出结果:This is eat   ,只有是存在继承关系才能这种方式转换         

  }

}

 

 

多态注意事项:

1、多态是方法的多态、属性没有多态;

2、父类和子类  有联系才能类型转换

3、存在条件:继承关系、方法需要重写,父类引用指向子类对象;

 

instanceof  :判断一个对象是什么类型,判断两个类是否存在父子关系

实例:

public static void main(String[] args){

  //Object>String                         object类是所有类的父类

  //Obejct >Person>Student;Obejct>Person>Teacher

  Object object =new Student();

  System.out.println(object instanceof Student);     //true

  System.out.println(object instanceof  Person);     //true

  System.out.println(object instanceof  Obejct);  //true

  System.out.println(object instanceof Teacher);  //true

       System.out.println(object instanceof String);  //true

---------------------------

  Person person=new Student();

  System.out.println(person instanceof Student);     //true

  System.out.println(person instanceof  Person);     //true

  System.out.println(person instanceof  Obejct);  //true

  System.out.println(person instanceof Teacher);  

  //false ,person引用指向的是Student,但Student与Teacher类是兄弟类,person与Teacher存在关系

       System.out.println(person instanceof String);  

  //编译出错,代码错误。String与Student一点关系都没有

 

---------------------------

  Student student=new Student();

  System.out.println(student instanceof Student);     //true

  System.out.println(student instanceof  Person);     //true

  System.out.println(student instanceof  Obejct);  //true

  System.out.println(student instanceof Teacher);  

  ////编译出错,代码错误。String与Student一点关系都没有

       System.out.println(person instanceof String);  

  //编译出错,代码错误。String与Student一点关系都没有

}

 

 

公式: System .out.println(x instanceof y);   

//编译能不能通过在于x与y是不是父子关系,若是则编译通过

//当编译通过,就看new 的对象与y有没有关系,若有则true;

例如上面的

Person person =new Student();

 System.out.println(person instanceof Teacher);   

//虽然编译能通过但是student与teacher没直接关系,所以false

 

 

 

posted @ 2019-12-07 19:09  未来我做主  阅读(343)  评论(0编辑  收藏  举报