求类的思想问题 用一棵树把类表示出来,想半天还没想好,哪位大神帮帮忙类 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 #package resources;import java.util.ArrayList;import java.util.List;abstract class Component { protected String name; public Component(String name) { this.name = name; } abstract void add(Component c); abstract void remove(Component c); abstract void display();}class Leaf extends Component { public Leaf(String name) { super(name); } @Override public void add(Component c) { } @Override public void remove(Component c) { } @Override public void display() { System.out.println(new String("--") + name); }}class Composite extends Component { public Composite(String name) { super(name); } private List<Component> childen = new ArrayList<Component>(); @Override public void add(Component c) { childen.add(c); } @Override public void remove(Component c) { childen.remove(c); } @Override public void display() { System.out.println(new String("-") + name); for (Component c : childen) { c.display(); } }}public class TestComposite { public static void main(String[] args) { Composite root = new Composite("root"); root.add(new Leaf("Leaf 1")); root.add(new Leaf("leaf 2")); Composite comp = new Composite(" X"); comp.add(new Leaf("leaf x1")); comp.add(new Leaf("leaf x2")); root.add(comp); root.display(); }} 楼主说的类是自己创造的类还是java自定义好的类? 根是Object,然后有很多类和接口的,自己动手找找了 API帮助文档里,各个类之间层次关系,慢慢看吧。。 同楼上 楼主自己看API文档 说的太好了,所有的类都是object类的子类 问一个关于protected修饰符的问题。 Eclipse插件项目中的一个小问题 java中如何把string型的001转为int型的001 随便看看 修改数据库记录一直报语法出错 排序??为什么总是溢出? 求一Java 正则表达式 是不是我没生成一个是例对象都会相应的生成一个key和一个hashcode啊 我想在服务器端返回一个对象.出现了"Error in reading object" 請問幾個在Applet中處理文字的問題 java哪方面需求高适合女生 《JAVA编程思想第4版》不懂的地方——望共同探讨
import java.util.List;abstract class Component {
protected String name; public Component(String name) {
this.name = name; } abstract void add(Component c); abstract void remove(Component c); abstract void display();
}class Leaf extends Component {
public Leaf(String name) {
super(name);
} @Override
public void add(Component c) { } @Override
public void remove(Component c) { } @Override
public void display() {
System.out.println(new String("--") + name);
}
}class Composite extends Component {
public Composite(String name) {
super(name);
} private List<Component> childen = new ArrayList<Component>(); @Override
public void add(Component c) {
childen.add(c); } @Override
public void remove(Component c) {
childen.remove(c); } @Override
public void display() {
System.out.println(new String("-") + name);
for (Component c : childen) {
c.display();
}
}
}public class TestComposite {
public static void main(String[] args) {
Composite root = new Composite("root");
root.add(new Leaf("Leaf 1"));
root.add(new Leaf("leaf 2")); Composite comp = new Composite(" X");
comp.add(new Leaf("leaf x1"));
comp.add(new Leaf("leaf x2"));
root.add(comp); root.display();
}}