测试Java的继承

From Yefu's notes
Jump to: navigation, search

1. Jave是没有C#里面的override关键字的:

public class test {

	public static void main(String[] args){

		test_base base = new test_derived();
		base.func();
	}
}

public class test_base {

	public void func(){
		System.out.println("Base func");
	}
}

public class test_derived extends test_base {
	public void func(){
		System.out.println("test_derived func");
		return 1;
	}
}

这里代码在java里执行的是test_derived

2. 如果derived class和base class里面同名函数返回值不同,则编译报错。

3. 同样的代码在C#里,执行的是基类的:

   class Program
    {
        static void Main(string[] args)
        {
            test_base t = new test_derived();
            t.func();
            Console.ReadLine();
        }
    }

    class test_base
    {
        public void func()
        {
            Console.WriteLine("test_base func");
        }
    }

    class test_derived : test_base
    {
        public void func()
        {
            Console.WriteLine("test_derived func");
        }
    }

但是编译的时候有一个warning:

'ConsoleApplication1.test_derived.func()' hides inherited member 'ConsoleApplication1.test_base.func()'. Use the new keyword if hiding was intended.


4. 还是在C#里,加了new,warning没了:

    class Program
    {
        static void Main(string[] args)
        {
            test_base t = new test_derived();
            t.func();
            Console.ReadLine();
        }
    }

    class test_base
    {
        public void func()
        {
            Console.WriteLine("test_base func");
        }
    }

    class test_derived : test_base
    {
        public new void func()
        {
            Console.WriteLine("test_derived func");
        }
    }


5. C#中,加了override关键字,报错:

    class Program
    {
        static void Main(string[] args)
        {
            test_base t = new test_derived();
            t.func();
            Console.ReadLine();
        }
    }

    class test_base
    {
        public void func()
        {
            Console.WriteLine("test_base func");
        }
    }

    class test_derived : test_base
    {
        public override void func()
        {
            Console.WriteLine("test_derived func");
        }
    }

'ConsoleApplication1.test_derived.func()': cannot override inherited member 'ConsoleApplication1.test_base.func()' because it is not marked virtual, abstract, or override

然后abstract和virtual的差别,你懂的。

在C++里:

#include "stdafx.h"

#include <iostream>
using namespace std;

class test_base
{
public:

	 void func()
	{
		cout<<"base "<<endl;
	}
};

class test_derived:public test_base
{
public:

	void func()
	{
		cout<<"derived "<<endl;
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	test_derived d;
	test_base *b = &d;

	b->func();//base

	int i;
	cin>>i;
	return 0;
}


如果加上virtual:

// cpptest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"

#include <iostream>
using namespace std;

class test_base
{
public:

	virtual void func()
	{
		cout<<"base "<<endl;
	}
};

class test_derived:public test_base
{
public:

	void func()
	{
		cout<<"derived "<<endl;
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	test_derived d;
	test_base *b = &d;

	b->func(); //derived

	int i;
	cin>>i;
	return 0;
}