java09

tech2022-08-03  128

封装

高内聚,低耦合 信息隐藏 重点:属性私有,get/set

package com.texing.demo01; public class Student { private String name; private int id; private char sex; //提供一些可以操作这个属性的方法 //提供一些public的get、set方法 public String getName(){ return this.name; } //set给这个数据设置值 public void setName(String name){ this.name=name; }//alt+insert public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } public int getId() { return id; } public void setId(int id) { if (id>100&&id<12000) { this.id = id; } else { this.id=0; } } } package com.texing; import com.texing.demo01.Student; public class App { public static void main(String[] args) { Student s1=new Student(); s1.setName("Peter"); System.out.println(s1.getName()); s1.setId(120); System.out.println(s1.getId()); } }
最新回复(0)