`
riss
  • 浏览: 15143 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

多种语言--override实现对比(C++,Java,Pascal)

阅读更多
这也许用JAVA确实不好说,不如用PASCAL说明要好理解一些(下面的是在DELPHI7中写的):

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  A = class
  public
    procedure Method1;virtual;
    procedure Method2;overload;virtual;
    procedure Method2(StrParam:String);overload;virtual;
  end;
  B = class(A)
  public
    procedure Method1;overload;override;
    procedure Method1(StrParam:String);overload;//这里不能用override;因为Method1(StrParam:String);在A中找不到
    procedure Method2;overload;override;
    procedure Method2(StrParam:String);overload;override;
    //这里可以有override;因为Method2(StrParam:String);在A中可以找到,
    //没有则属于'覆盖',即在方法中不会有inherited,当然会收到一个警告:
    //[Warning] Unit1.pas(19): Method 'Method2' hides virtual method of base type 'A'
    }
  end;
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ A }

procedure A.Method1;
begin
  ShowMessage('A:Mehtod1(No Parameter)');
end;

procedure A.Method2;
begin
  ShowMessage('A:Mehtod2(No Parameter)');

end;

procedure A.Method2(StrParam: String);
begin
  ShowMessage('A:Mehtod2(String Parameter)');

end;

{ B }

procedure B.Method1;
begin
  inherited; //super.Mehtod1;
  ShowMessage('B:Mehtod1(No Parameter)');
end;

procedure B.Method1(StrParam: String);
begin
  inherited Method1;//这里仍然可以强制使用A中的Method1;super.Mehtod1;这与B.Method2(StrParam: String);的inherited不同
  ShowMessage('B:Mehtod1(String Parameter)');

end;

procedure TForm1.Button1Click(Sender: TObject);
var
  temp:B;
begin
  temp:=B.Create;
  temp.Method1();
  temp.Method1('riss');
  temp.Method2;
  temp.Method2('riss');
  temp.Free;
end;

procedure B.Method2;
begin
  inherited;//super.Mehtod2;
  ShowMessage('B:Mehtod2(No Parameter)');

end;

procedure B.Method2(StrParam: String);
begin
  inherited;//super.Mehtod2(StrParam);
  ShowMessage('B:Mehtod2(String Parameter)');

end;

end.

啰嗦了点,将就看看吧!PASCAL做为一门教学语言还真的不错!哈哈!
------------------------------------------------------------------------------------------------------------------------------------------
与之对应的C++代码较为难看些,不好理解


代码
 // ControlTest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>

using namespace std ;

class A
{
public:
	virtual void Method1(void);
	virtual void Method2(void);
	virtual void Method2(string StrParam);
};
class B:public A
{

public:
	void Method1(void);
	void Method1(string StrParam);
	void Method2(void);
	void Method2(string StrParam);
};
int _tmain(int argc, _TCHAR* argv[])
{
	int i;

	A* a = new A();
	a->Method1();
	a->Method2();
	a->Method2("riss");

	B* b = new B();
	b->Method1();
	b->Method1("riss");
	b->Method2();
	b->Method2("riss");

	cin >>i;
	delete a;
	delete b;
	return 0;
}

void A::Method1(void)
{
	cout << "A:Method1(No Parameter)"<<endl;
}

void A::Method2(void)
{
	cout << "A:Method2(No Parameter)"<<endl;
}

void A::Method2(string StrParam)
{
	cout << "A:Method2(String Parameter)"<<endl;
}

void B::Method1(void)
{
	((A)* this).Method1();
	cout << "B:Method1(No Parameter)"<<endl;

}

void B::Method1(string StrParam)
{
	((A)* this).Method1();
	cout << "B:Method1(Str Parameter)"<<endl;
}

void B::Method2(void)
{
	((A)* this).Method1();
	cout << "B:Method2(No Parameter)"<<endl;
}

void B::Method2(string StrParam)
{
	((A)* this).Method2(StrParam);
	cout << "B:Method2(String Parameter)"<<endl;
}


C++的语法忘记的差不多了,上面的得出结果差不多,其他的另当别论(比如说,代码的风格,质量等等)
------------------------------------------------------------------------------------------------------------------------------------------
最后看看JAVA的实现,比C++要稍好一点,但还是没有DELPHI来的直观.
但从另一个角度来看,我们这里所讨论的继承,并不能说关于JAVA的继承,这是面方对象中的继承啊!
我还是找一本对深讨面向对象方面的书翻翻再来和大家继续哦!(温故而知新)
C#的和DELPHI+JAVA的写法差不多(不给出了)


代码
 package com;

class A{
	void Method1(){
		System.out.println("A:Mehtod1(No Parameter)");
	}
	void Method2(){
		System.out.println("A:Mehtod2(No Parameter)");		
	}
	void Method2(String strParam){
		System.out.println("A:Mehtod2(String Parameter)");		
	}
}

class B extends A{
	void Method1(){
		super.Method1();
		System.out.println("B:Mehtod1(No Parameter)");
	}
	void Method1(String strParam){
		super.Method1();
		System.out.println("B:Mehtod1(No Parameter)");		
	}
	void Method2(){
		super.Method2();
		System.out.println("B:Mehtod2(No Parameter)");		
	}
	void Method2(String strParam){
		super.Method2(strParam);
		System.out.println("B:Mehtod2(String Parameter)");		
	}
	
}
public class BBS_Override {

	/**
	 * <p>******************************************************************************</p>
	 * <p>Method Name:main</p>
	 * 
	 * <p>Function:</p>
	 *
	 * <p>Description:</p>
	 * <p></p>
	 * @param args
	 * <p>******************************************************************************</p>
	 */

	public static void main(String[] args) {
		A a=new A();
		a.Method1();
		a.Method2();
		a.Method2("riss");
		
		B b=new B();
		b.Method1();
		b.Method1("riss");
		b.Method2();
		b.Method2("riss");
	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics