package com.oop.Demo03;
public class Application {
public static void main(String[] args) {
Pet dog = new Pet();
dog.name = "qwe";
dog.age = 12;
dog.shout();
System.out.println(dog.name);
System.out.println(dog.age);
Pet cat = new Pet();
cat.shout();
}
}
输出:
叫了一声
qwe
12
叫了一声
进程已结束,退出代码 0
package com.oop.Demo03;
public class Pet {
String name;
int age;
//自含 无参构造,也就是说不用再public class pet(String name) 了
public void shout() {
System.out.println("叫了一声");
}
}
原文:https://www.cnblogs.com/function123/p/13472230.html