继承时的成员访问权限 [英] Member Access When Inheriting

查看:58
本文介绍了继承时的成员访问权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使一个类的成员必须覆盖_new_

定义?例如,当继承自

System.Collections.CollectionBase时,您需要实现某些

方法,例如public void Add(MyClass c)。


如何在master类中强制执行相同的行为(需要在继承的类中实现具有

a新返回类型的成员)(类似于

CollectionBase)?


我有一个名为LinearNavigator的类。这是

前进和后退操作(如IE)的通用管理器,它继承用于提供与特定类型的交互(例如继承CollectionBase)。

LinearNavigator有方法Next&回到哪个我想强制要求

工具和一个名为Clear的方法,不应该是
重写/重新实现。


谢谢!

解决方案

您好,

您需要将LinearNavigator定义为抽象类。

您可以正常包含方法和属性,但是您希望强制实施的

方法,

将它们定义为抽象。当然,这些不能包含

基类中的任何实现:


继承类必须使用override关键字。如果

这个被省略,或者省略了实现,

编译器报告错误。


例如:


///基类

公共抽象类CMostlyAbstract

{

public int x = 1;

public CMostlyAbstract()

{

// TODO:添加构造函数逻辑

}

public abstract void MustOverride();


public void清除()

{

x = 0;

}

}

///覆盖类

公共类COverride:CMostlyAbstract

{

public COverride()

{

// TODO:添加构造函数逻辑

}


public void重置()

{

base.Clear();

}

public override void MustOverride()

{

//具体实现在这里!

}

}

///使用

COverride co = new COverride();

co.Mu stOverride();

co.Clear(); //调用基本方法

co.Reset(); //调用COverride,调用base

HTH,


Martin

-----原帖-----
你怎么做使用_new_definition覆盖
的强制类的成员?例如,当继承自System.Collections.CollectionBase时,您需要
实现某些方法,例如public void Add(MyClass c)。

如何强制执行主
类(类似于theCollectionBase)中的相同行为(要求
在继承类中实现一个具有新返回类型的成员)?

我有一个名为 ; LinearNavigator"这是
泛型和后退操作(如IE)的通用管理器,继承
以提供与特定类型的交互(如继承
CollectionBase).LinearNavigator有方法Next&返回我想要
强制执行和一个名为Clear的方法,不应该被覆盖/重新实现。

谢谢!



那么,哪个最好在抽象类和vica上使用接口
$ b $反之亦然?

mk < mk@nospam.com>在留言中写道

news:00 **************************** @ phx.gbl ... < blockquote class =post_quotes>您好,
您需要将LinearNavigator定义为抽象类。
您可以正常包含方法和属性,但是您希望强制实施的方法和属性,
将它们定义为抽象。当然这些不能包含基类中的任何实现:

继承类必须使用override关键字。如果省略,或省略实现,
编译器报告错误。

例如:

///基类 {public int x = 1;
public CMostlyAbstract()
// TODO:添加构造函数逻辑

公共抽象void MustOverride();

public void清除()
{
x = 0;
}
} ///覆盖类
公共类COverride:CMostlyAbstract
公共COverride()
//
// TODO:添加构造函数逻辑
}

public void重置()
{
base.Clear();
}
公共覆盖void MustOverride()
{
//具体实施在这里!
}
//
使用
COverride co = new COverride();
co.MustOverride();
co.Clear(); //调用基本方法
co.Reset(); //调用COverride,调用base

HTH,

Martin

-----或原始消息-----
如何使一个类的成员必须


覆盖_new_

定义?例如,当继承自System.Collections.CollectionBase时,您需要


实现某些

方法,例如public void Add(MyClass c)。

如何强制执行相同的行为(要求


实现

一个继承类中的新返回类型的成员) master


类(类似于

CollectionBase)?

我有一个名为LinearNavigator的类。这是

继承的前进和后退操作(如IE)的


泛型管理器


提供

与特定类型的交互(如继承


CollectionBase)。

LinearNavigator有方法Next&返回我想要


强制要求

实现和一个名为Clear的方法,不应该被覆盖/重新实现。

谢谢!



http://msdn.microsoft.com/library/de...us/ vbcon / html /

vbconabstractclassesversusinterfaces.asp


找到抽象类vs界面问题的链接:D

Jack Meyhoff < PO ******** @ 127.0.0.3>在消息中写道

新闻:O3 ************** @ TK2MSFTNGP09.phx.gbl ...

那么,它会在哪里最好在抽象类和vica上使用接口


" mk" < mk@nospam.com>在消息中写道
新闻:00 **************************** @ phx.gbl ...

您好,
您需要将LinearNavigator定义为抽象类。
您可以正常包含方法和属性,但是您希望强制实施的方法,将它们定义为抽象。当然这些不能包含基类中的任何实现:

继承类必须使用override关键字。如果省略,或省略实现,
编译器报告错误。

例如:

///基类 {public int x = 1;
public CMostlyAbstract()
// TODO:添加构造函数逻辑

公共抽象void MustOverride();

public void清除()
{
x = 0;
}
} ///覆盖类
公共类COverride:CMostlyAbstract
公共COverride()
//
// TODO:添加构造函数逻辑
}

public void重置()
{
base.Clear();
}
公共覆盖void MustOverride()
{
//具体实施在这里!
}
//
使用
COverride co = new COverride();
co.MustOverride();
co.Clear(); //调用基本方法
co.Reset(); //调用COverride,调用base

HTH,

Martin

-----或原始消息-----
如何使一个类的成员必须


覆盖_new_

定义?例如,当继承自System.Collections.CollectionBase时,您需要


实现某些

方法,例如public void Add(MyClass c)。

如何强制执行相同的行为(要求


实现

一个继承类中的新返回类型的成员) master


类(类似于

CollectionBase)?

我有一个名为LinearNavigator的类。这是

继承的前进和后退操作(如IE)的


泛型管理器


提供

与特定类型的交互(如继承


CollectionBase)。

LinearNavigator有方法Next&返回我想要


强制要求

实现和一个名为Clear的方法,不应该被覆盖/重新实现。

谢谢!




How do you make a member of a class mandatory to override with a _new_
definition? For example, when inheriting from
System.Collections.CollectionBase, you are required to implement certain
methods, such as public void Add(MyClass c).

How can I enforce the same behavior (of requiring to implement a member with
a new return type in an inherited class) in the master class (similar to the
CollectionBase)?

I have a class called "LinearNavigator" which is a generic manager for
Forward and Back operations (like IE) that is inherited to provide
interaction with specific types (like inheriting CollectionBase).
LinearNavigator has methods Next & Back which I want to make mandatory to
implement and a method called Clear that should not be
overridden/re-implemented.

Thanks!

解决方案

Hi,
You need to define LinearNavigator as an abstract class.
You can contain methods and properties as normal, but the
methods for which you wish to enforce implementation,
define them as abstract. Naturally these cannot contain
any implementation in the base class:

The inheriting class must use the override keyword. If
this is omitted, or the implementation is omitted the
compiler reports an error.

For example:

/// Base Class
public abstract class CMostlyAbstract
{
public int x = 1;
public CMostlyAbstract()
{
// TODO: Add constructor logic
}
public abstract void MustOverride();

public void Clear()
{
x=0;
}
}
/// Overriding class
public class COverride : CMostlyAbstract
{
public COverride()
{
// TODO: Add constructor logic
}

public void Reset()
{
base.Clear();
}
public override void MustOverride()
{
// Specific Implementation here!
}
}
/// Usage
COverride co = new COverride();
co.MustOverride();
co.Clear();// Call base method
co.Reset();// call COverride, which calls base
HTH,

Martin

-----Original Message-----
How do you make a member of a class mandatory to override with a _new_definition? For example, when inheriting from
System.Collections.CollectionBase, you are required to implement certainmethods, such as public void Add(MyClass c).

How can I enforce the same behavior (of requiring to implement a member witha new return type in an inherited class) in the master class (similar to theCollectionBase)?

I have a class called "LinearNavigator" which is a generic manager forForward and Back operations (like IE) that is inherited to provideinteraction with specific types (like inheriting CollectionBase).LinearNavigator has methods Next & Back which I want to make mandatory toimplement and a method called Clear that should not be
overridden/re-implemented.

Thanks!
.



So,where would it best to use an Interface over an abstract class and vica
versa?
"mk" <mk@nospam.com> wrote in message
news:00****************************@phx.gbl...

Hi,
You need to define LinearNavigator as an abstract class.
You can contain methods and properties as normal, but the
methods for which you wish to enforce implementation,
define them as abstract. Naturally these cannot contain
any implementation in the base class:

The inheriting class must use the override keyword. If
this is omitted, or the implementation is omitted the
compiler reports an error.

For example:

/// Base Class
public abstract class CMostlyAbstract
{
public int x = 1;
public CMostlyAbstract()
{
// TODO: Add constructor logic
}
public abstract void MustOverride();

public void Clear()
{
x=0;
}
}
/// Overriding class
public class COverride : CMostlyAbstract
{
public COverride()
{
// TODO: Add constructor logic
}

public void Reset()
{
base.Clear();
}
public override void MustOverride()
{
// Specific Implementation here!
}
}
/// Usage
COverride co = new COverride();
co.MustOverride();
co.Clear();// Call base method
co.Reset();// call COverride, which calls base
HTH,

Martin

-----Original Message-----
How do you make a member of a class mandatory to


override with a _new_

definition? For example, when inheriting from
System.Collections.CollectionBase, you are required to


implement certain

methods, such as public void Add(MyClass c).

How can I enforce the same behavior (of requiring to


implement a member with

a new return type in an inherited class) in the master


class (similar to the

CollectionBase)?

I have a class called "LinearNavigator" which is a


generic manager for

Forward and Back operations (like IE) that is inherited


to provide

interaction with specific types (like inheriting


CollectionBase).

LinearNavigator has methods Next & Back which I want to


make mandatory to

implement and a method called Clear that should not be
overridden/re-implemented.

Thanks!
.



http://msdn.microsoft.com/library/de...us/vbcon/html/
vbconabstractclassesversusinterfaces.asp

Found a link to the abstract class vs interface question :D
"Jack Meyhoff" <po********@127.0.0.3> wrote in message
news:O3**************@TK2MSFTNGP09.phx.gbl...

So,where would it best to use an Interface over an abstract class and vica
versa?
"mk" <mk@nospam.com> wrote in message
news:00****************************@phx.gbl...

Hi,
You need to define LinearNavigator as an abstract class.
You can contain methods and properties as normal, but the
methods for which you wish to enforce implementation,
define them as abstract. Naturally these cannot contain
any implementation in the base class:

The inheriting class must use the override keyword. If
this is omitted, or the implementation is omitted the
compiler reports an error.

For example:

/// Base Class
public abstract class CMostlyAbstract
{
public int x = 1;
public CMostlyAbstract()
{
// TODO: Add constructor logic
}
public abstract void MustOverride();

public void Clear()
{
x=0;
}
}
/// Overriding class
public class COverride : CMostlyAbstract
{
public COverride()
{
// TODO: Add constructor logic
}

public void Reset()
{
base.Clear();
}
public override void MustOverride()
{
// Specific Implementation here!
}
}
/// Usage
COverride co = new COverride();
co.MustOverride();
co.Clear();// Call base method
co.Reset();// call COverride, which calls base
HTH,

Martin

-----Original Message-----
How do you make a member of a class mandatory to


override with a _new_

definition? For example, when inheriting from
System.Collections.CollectionBase, you are required to


implement certain

methods, such as public void Add(MyClass c).

How can I enforce the same behavior (of requiring to


implement a member with

a new return type in an inherited class) in the master


class (similar to the

CollectionBase)?

I have a class called "LinearNavigator" which is a


generic manager for

Forward and Back operations (like IE) that is inherited


to provide

interaction with specific types (like inheriting


CollectionBase).

LinearNavigator has methods Next & Back which I want to


make mandatory to

implement and a method called Clear that should not be
overridden/re-implemented.

Thanks!
.




这篇关于继承时的成员访问权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆