如图所示:
package oop.Demo03; public class Pet { public String name; public int age; //无参构造--一旦有了有参构造方法,必须显示无参构造方法! //此处的方法可以忽略 public Pet() { } public void shout(){ System.out.println("叫一声!"); } }
package oop; import oop.Demo03.Pet; public class Application { public static void main(String[] args) { Pet dog = new Pet(); dog.name = "小黄"; dog.age = 3; dog.shout(); System.out.println(dog.name); System.out.println(dog.age); } }