public class Q3 { String brand; double price; double size; public void speak(){ System.out.println("品牌是:" + brand); System.out.println("价格是:" + price); System.out.println("尺寸是:" + size); } public void eat() { System.out.println("可以用来打电话"); } public void call() { System.out.println("可以用来打游戏"); } @Override public String toString() { return "Phone{" + "brand='" + brand + '\'' + ", price=" + price + ", size=" + size + '}'; } }
import java.util.Arrays; public class Q4 { public static void main(String[] args) { Q3 p1 = new Q3(); p1.brand = "苹果"; p1.price = 5000; p1.size = 5.6; p1.speak(); p1.eat(); p1.call(); System.out.println("----------"); Q3 p2 = new Q3(); p2.brand = "小米"; p2.price = 6000; p2.size = 6.4; p2.speak(); p2.eat(); p2.call(); Q3[] arr = new Q3[2]; arr[0] = p1; arr[1] = p2; String arr1 = Arrays.toString(arr); System.out.println(arr1); } }