1 //类 2 public class Student { 3 private String name; 4 private int age; 5 public void setName(String name) 6 { 7 this.name = name; 8 } 9 public String getName() 10 { 11 return this.name; 12 } 13 public Student(){}//默认构造 14 public void say(){ 15 System.out.println(this.name+"在说话");//this指针的调用 16 } 17 } 18 //main 19 public class Main { 20 public static void main(String[] args){ 21 Student s1 = new Student(); 22 s1.setName("不良帅"); 23 String name = s1.getName(); 24 System.out.println(name); 25 } 26 } 27 //用private封装 28 //用get()和set()来提供设置和获取接口 29 //alt+insert 选中get,set可以自动添加
标签:封装,String,22,private,2023.7,Student,public,name From: https://www.cnblogs.com/muzhaodi/p/17574188.html