public class LvMengDi { public static void main(String[] args) { Student lry = new Student("李瑞英",18, 0); Student ldy = new Student("黎冬燕",17,0); lry.selfInfo(); ldy.selfInfo(); Student lll=new Student(); lll.name ="lll";//name未封装 System.out.println(ldy.getAge()); lry.setName("liruiying"); } }
public class Auto { //封装 int speed; Auto(int speed){ this.speed=speed; } Auto(){} void start(){ System.out.println("Car start"); } void speedup(){ System.out.println("Car Speedup"); } void stop(){ System.out.println("Car stop"); } }
public class Student { String name; int age; int sex; //构造方法 Student(){} Student(String name,int age,int sex){ this.name=name; this.age= age; this.sex=sex; } void sayHello(){ System.out.println("hello"); } void selfInfo(){ System.out.println("name="+this.name +",age="+this.age +",sex"+this.sex); } //私有属性setter方法和 getter方法 void setName(String name){ this.name=name; } int getAge(){ return this.age; } }