public class Student02 { private String name; private int age; public void setName(String n){ name = n; } public String getName(){ return name; } public void setAge(int a){ age = a; } public int getAge(){ return age; } public void show(){ System.out.println(name + "," +age); } }
public class Student02Demo { public static void main(String[] args) { Student02 s =new Student02(); s.setName("徐凤年"); s.setAge(18); s.show(); //使用get方法输出 System.out.println(s.getName() + "," +s.getAge()); } }
一般可以使用两种不同的方式输出,一种是get一种是set,均在第一个代码中体现
标签:String,私有,void,笔记,新人,int,age,public,name From: https://www.cnblogs.com/hxh-/p/17639009.html