Java 编程:如何使用一个方法展示多个类信息 - 实例解析
package day9.work; //\u539f\u59cb\u7684\u4e3b\u7a0b\u5e8f\u6587\u4ef6\u540d\u79f0 public class Kitchen { public static void main(String[] args) { Refrigerator ref = new Refrigerator("\u9ec4\u8c46","\u725b\u8089"); Knife k = new Knife(20,1); Pot p = new Pot("\u94c1\u9505",2); p.refrigerator = ref; p.knife = k; p.show(); } } class Refrigerator { String vegetable; String meat; public Refrigerator(String vegetable, String meat) { this.vegetable = vegetable; this.meat = meat; } public void show() { System.out.println("\u51b0\u7bb1\u91cc\u6709\uff1a" + vegetable + "\u548c" + meat); } } class Knife { int size; int quantity; public Knife(int size, int quantity) { this.size = size; this.quantity = quantity; } public void show() { System.out.println("\u5200\u7684\u5927\u5c0f\uff1a" + size + "\u514b\u7c73\uff0c\u6570\u91cf\uff1a" + quantity); } } class Pot { String type; int capacity; Refrigerator refrigerator; Knife knife; public Pot(String type, int capacity) { this.type = type; this.capacity = capacity; } public void show() { System.out.println("\u9505\u7684\u7c7b\u578b\uff1a" + type + "\uff0c\u5bb9\u91cf\uff1a" + capacity + "\u5347"); refrigerator.show(); knife.show(); } } \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728 Pot \u7c7b\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a show() \u65b9\u6cd5\uff0c\u5e76\u5728 show() \u65b9\u6cd5\u4e2d\u901a\u8fc7\u8c03\u7528 Refrigerator \u7c7b\u7684 show() \u65b9\u6cd5\u548c Knife \u7c7b\u7684 show() \u65b9\u6cd5\u6765\u8f93\u51fa\u76f8\u5e94\u7684\u4fe1\u606f\u3002\u7136\u540e\u5728 Kitchen \u7c7b\u7684 main() \u65b9\u6cd5\u4e2d\u901a\u8fc7\u521b\u5efa\u5bf9\u8c61\u5e76\u8c03\u7528 p.show() \u6765\u5b9e\u73b0\u8f93\u51fa\u3002
原文地址: https://www.cveoy.top/t/topic/px4V 著作权归作者所有。请勿转载和采集!