面向对象的思想来的, 打个比方:房子是人进的,那么房子不会管进门的是男人,还是女人. ------------------------------------------ interface class A { void str(); } 这是人. ------------------------------- class B implements A { pulic void str(); { //程序代码 { } 这是男人。 ------------------------ class C { public void Cstr(A a) { a.str(); } } 这是房子。 房子里有一个门 public void Cstr(A a),它对你说只有A(人)能进。 ---------------------------------------------- class D { pulic static viod main (string[] ages) { C c = new C(); B b=new B(); c.str(b);//这里告诉程序我是B(男人,但男人也是人,所以我能进) } } ------------------------------------ 为什么class B implements A除去了implements A,就会抱错 因为没有了implements A 就表示它不是人,所以不能进,c.str(b)就会报错。^-^
TO gzhiceberg(天晓得) B b=new B();b是B一个对象。他就有这个str()方法对把,传进去就是B这个方法是吧 ------------------------------------------------------------------------- 是调用b的str方法,java里面都是动态绑定.^-^
in fact, your problem is not how to transfer parameter,it's a problem of inherite!Please examine the following code and notice the relation of A and B class A { public A() { System.out.println("this is instance of a"); } } class B extends A { public B() { System.out.println("this is instance of b"); } } class test { public static void main(String[] arg) { A a=new A(); } } Compile it and run,the result is:this is instance of a contine examining the following code: class A { public A() { System.out.println("this is instance of a"); } } class B extends A { public B() { System.out.println("this is instance of b"); } } class test { public static void main(String[] arg) { B b=new B(); } } the result is:this instance of a this instance of b then you can see when you instance the class B(SubClass),the first thing of the System is instance the class A(SuperClass)! and so,how to instance a class?how can you say a class is instanced?maybe it's constructor method has executed! let's continue: class A { public A() { System.out.println("this is instance of a"); } } class B extends A { public B() { System.out.println("this is instance of b"); } } class test { public static void main(String[] arg) { B b=new B(); A a=new A(); a=b; } } the code will be compile and run well,but class A { public A() { System.out.println("this is instance of a"); } } class B extends A { public B() { System.out.println("this is instance of b"); } } class test { public static void main(String[] arg) { B b=new B(); A a=new A(); b=a; } } the Java interpreter will produce the following error:test.java:21:incompatible types found:A request:B b=a; ~ why?you can think yourself!
2、b实际上就是B类的一个对象。
3、如果你把B的implements A去掉,那么B类就没有实现A类的方法,所以c.str(b)就会出错啦。
打个比方:房子是人进的,那么房子不会管进门的是男人,还是女人.
------------------------------------------
interface class A
{
void str();
}
这是人.
-------------------------------
class B implements A
{
pulic void str();
{
//程序代码
{
}
这是男人。
------------------------
class C
{
public void Cstr(A a)
{
a.str();
}
}
这是房子。
房子里有一个门 public void Cstr(A a),它对你说只有A(人)能进。
----------------------------------------------
class D
{
pulic static viod main (string[] ages)
{
C c = new C();
B b=new B();
c.str(b);//这里告诉程序我是B(男人,但男人也是人,所以我能进)
}
}
------------------------------------
为什么class B implements A除去了implements A,就会抱错
因为没有了implements A 就表示它不是人,所以不能进,c.str(b)就会报错。^-^
但去掉了implements A,B 就不再是A的实例,所有会报错。
B b=new B();b是B一个对象。他就有这个str()方法对把,传进去就是B这个方法是吧
我看继承了的,就是b到第传进去的什么啊
哪个类new的就是哪个类的对象吧
http://blog.csdn.net/yangbc/archive/2005/02/18/292660.aspx
B b=new B();b是B一个对象。他就有这个str()方法对把,传进去就是B这个方法是吧
-------------------------------------------------------------------------
是调用b的str方法,java里面都是动态绑定.^-^
class A
{
public A()
{
System.out.println("this is instance of a");
}
}
class B extends A
{
public B()
{
System.out.println("this is instance of b");
}
}
class test
{
public static void main(String[] arg)
{
A a=new A();
}
}
Compile it and run,the result is:this is instance of a
contine examining the following code:
class A
{
public A()
{
System.out.println("this is instance of a");
}
}
class B extends A
{
public B()
{
System.out.println("this is instance of b");
}
}
class test
{
public static void main(String[] arg)
{
B b=new B();
}
}
the result is:this instance of a
this instance of b
then you can see when you instance the class B(SubClass),the first thing of the System is instance the class A(SuperClass)!
and so,how to instance a class?how can you say a class is instanced?maybe it's constructor method has executed!
let's continue:
class A
{
public A()
{
System.out.println("this is instance of a");
}
}
class B extends A
{
public B()
{
System.out.println("this is instance of b");
}
}
class test
{
public static void main(String[] arg)
{
B b=new B();
A a=new A();
a=b;
}
}
the code will be compile and run well,but
class A
{
public A()
{
System.out.println("this is instance of a");
}
}
class B extends A
{
public B()
{
System.out.println("this is instance of b");
}
}
class test
{
public static void main(String[] arg)
{
B b=new B();
A a=new A();
b=a;
}
}
the Java interpreter will produce the following error:test.java:21:incompatible types
found:A
request:B
b=a;
~
why?you can think yourself!