面向对象三大特性

封装

一般封装的是属性,基本上不会去封装方法,但方法也可以。

一般是在属性前面加,private即可。

记住这句话:属性私有,get/set

提供一些public的get,set方法,操作这个属性。

get 获得这个值(输出)

set操作这个值(输入)

列如:

package bao;

public class Application {
   public static void main(String[] args) {
       Student student = new Student();
    student.getName( "付");
    student.setName( "付");

  }
}


package bao;

public class Student {
   private String name;
   public int id;

   public String getName(String name){
       this.name = name;
       System.out.println(this.name);
       return this.name;
  }
   public String setName(String name) {
       this.name = "毅";
       System.out.println(this.name);
       return name;
  }
}

 

 

 

继承

 

 

 

posted @ 2022-07-31 21:34  锦书南辞  阅读(30)  评论(0)    收藏  举报