1 package com.oop.demo03; 2 3 public class Pet { 4 public String name; 5 public int age; 6 7 //无参构造 8 9 public void shout() { 10 System.out.println("叫了一声"); 11 } 12 }
1 package com.oop; 2 3 import com.oop.demo03.Pet; 4 5 public class Application { 6 7 public static void main(String[] args) { 8 Pet dog = new Pet(); 9 dog.name = "小猪"; 10 dog.age = 3; 11 dog.shout(); 12 13 System.out.println(dog.name); 14 System.out.println(dog.age); 15 16 Pet cat = new Pet(); 17 18 } 19 20 }