If I create several instances of a superclass, where their state is set to arbitrary values by the class constructor, how can I make sure a subclass inherits its state from a specific instance of the superclass?
3 に答える
0
サブクラスの各インスタンスには、そのスーパークラスの独自のインスタンスがあります。そう:
public class Super
{
protected int x;
public Super( int x ) { this.x = x; }
}
public class Sub
{
public Sub( int x ) { super( x ); }
public void func() { System.out.println( x ); }
public static void main( String[] args )
{
Sub a, b;
a = new Sub( 1 );
b = new Sub( 2 );
a.func(); b.func();
}
}
これは出力されます
1
2
于 2013-10-30T16:41:28.963 に答える