C++,C#,Java继承机制的不同(2)
3. 将test方法改为virtual方法
结果如下:
in father's constructor!
father's a: 10
father's b: father's b
father's test
out father's constructor
in son's constructor!
son's a: 10
son's b: son's b
son's test
out son's constructor
f.a : 10
f.b : father's b
son's test
Press any key to continue
结论如下:
virtual 方法(参数相同)调用时指代由子类的实现决定,但属性仍由对象的声明类型决定,this指代仍由当前类决定。
4. 子类的方法参数不同的情况
将子类的test方法改为:
void test(int i, float j);
结果如下:
in father's constructor!
father's a: 10
father's b: father's b
father's test
out father's constructor
in son's constructor!
son's a: 10
son's b: son's b
son's test
out son's constructor
f.a : 10
f.b : father's b
father's test
Press any key to continue
结论如下:
对于参数不同或者参数返回值不同的vitual方法,指代由父类决定
C#的实现与结果(编译器:.net 2003)
1. 代码:
using System;
namespace inheritence_test
{
/// <summary>
/// Summary description for inheritence.
/// </summary>
public class father
{
public father()
{
//
// TODO: Add constructor logic here
//
Console.WriteLine("in father's constructor!");
Console.WriteLine(this.x+"");
Console.WriteLine(this.str+"");
this.test(1,1);
Console.WriteLine("out father's constructor!\n");
}
public void test(int i,int j){
Console.WriteLine("this is father's test");
}
public int x=10;
public String str="father's string";
}
}
using System;
namespace inheritence_test
{
/// <summary>
///
/// </summary>
public class son : father
{
public son()
{
//
// TODO: Add constructor logic here
//
Console.WriteLine("in son's constructor!");
Console.WriteLine(this.x+"");
Console.WriteLine(this.str+"");
this.test(1,1);
Console.WriteLine("out son's constructor!\n");
}
public void test(int i,int j)
{
Console.WriteLine("this is son's test");
}
public int x=0;
public String str="son's string";
}
}
private void button1_Click(object sender, System.EventArgs e)
{
father f=new son();
Console.WriteLine("f.x : "+f.x+"\n");
Console.WriteLine("f.str : "+f.str+"\n");
f.test(1,1);
}
in father's constructor!
10
father's string
this is father's test
out father's constructor!
in son's constructor!
0
son's string
this is son's test
out son's constructor!
f.x : 10
f.str : father's string
this is father's test
2. 结论如下:
a. 无论针对父类的方法(非virtual)还是属性其指代均由指针或者对象的声明类型决定
b. this的指代由当前类型决定
3. 将test方法改为virtual方法
virtual public void test(int i,int j){
Console.WriteLine("this is father's test");
}
运行结果同上
将子类的方法声明为override
override public void test(int i,int j)
{
Console.WriteLine("this is son's test");
}
结果如下:
in father's constructor!
10
father's string
this is son's test
out father's constructor!
in son's constructor!
0
son's string
this is son's test
out son's constructor!
f.x : 10
f.str : father's string
this is son's test
4. 结论:
只有在子类使用了override关键字之后,才能实现C++的virtual结构,其他指代同c++
java的实现与结果(编译器: JB7 Visualage for java 3.5)
1. 代码:
public class father {
public father() {
System.out.println("in father's constructor!");
System.out.println(this.x+"");
System.out.println(this.str+"");
this.test(1,1);
System.out.println("out father's constructor!\n");
}
public void test(int i,int j){
System.out.println("this is father's test");
}
public int x=10;
public String str="father's string";
public static void main(String[] args) {
father father1 = new father();
}
}
package test;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class son extends father{
public son() {
System.out.println("in son's constructor!");
System.out.println(this.a+"");
System.out.println(this.b+"");
this.test(1,1);
System.out.println("out son's constructor!\n");
}
public int a=0;
public String b="son's b";
public void test(int i,int j){
System.out.println("this is son's test");
}
public static void main(String[] args) {
father f = new son();
System.out.println("f.x : "+f.x+"");
System.out.println("f.str : "+f.str+"");
f.test(1,1);
}
}
2. 结果(请注意this的指代):
in father's constructor!
10
father's string
this is son's test
out father's constructor!
in son's constructor!
0
son's b
this is son's test
out son's constructor!
f.x : 10
f.str : father's string
this is son's test
3. 结论:
在指代上同C++的virtual 方法,但this的指代由方法由实现的对象决定(这里是子类)属性由对象的类型决定(这里是父类)。