访问 Parents 个属性
Accessing Parents attributes
这可能是一个愚蠢的问题,但我是 C# 的新手。我想知道有没有办法直接在childclass中使用parent的属性。我做了很多 Eiffel,当一个 class 被一个或多个 classes 继承时(因为是的,Eiffel 没有接口,你可以继承多个 classes)。
就像那个例子:(埃菲尔语言)
Parent Class:
class Parent
features
int id
string lastName
Child Class:
class Child inherit
PARENT
feature
bool isChild
在那种情况下,Child class 已经可以访问 id 和 lastName 并且可以直接设置为 Child 属性的一部分,不必创建 Parent.
但到目前为止我做了这个(C# 语言):
Parent class:
public class Character
{
Int32 Id;
String name;
List<String> images;
public Character()
{
name = "";
images = null;
}
public Character(string a_name, List<String> imagePaths)
{
name = a_name;
images = imagePaths;
}
public Character(Int32 a_id, string a_name, List<String> imagePaths)
{
Id = a_id;
name = a_name;
images = imagePaths;
}
}
Child class:
public class NPC : Character
{
public bool isVender;
public NPC()
{
Character character = new Character();
isVender = false;
}
public NPC(string a_name, List<String> images)
{
Character caracter = new Character(a_name, images);
isVender = false;
}
public NPC(string a_name, List<string> images, bool a_bool)
{
Character caracter = new Character(a_name, images);
isVender = a_bool;
}
}
所以我的问题是,有没有办法像 Eiffel 一样在 C# 中直接访问 parent 的属性?
将要在子 类 中使用的字段声明为 protected
。详细了解受保护的可见性修饰符 here.
public class Character
{
protected Int32 Id;
protected String name;
protected List<String> images;
public Character()
{
name = "";
images = null;
}
public Character(string a_name, List<String> imagePaths)
{
name = a_name;
images = imagePaths;
}
public Character(Int32 a_id, string a_name, List<String> imagePaths)
{
Id = a_id;
name = a_name;
images = imagePaths;
}
}
然后你就可以在子类.
中使用受保护的字段了
你真正似乎想要的是使用基础class的构造函数来设置基础class的字段,这是一个C# 中的语法略有不同:
public class NPC : Character
{
public bool isVender;
public NPC() : base()
{
isVender = false;
}
public NPC(string a_name, List<String> images) : base(a_name, images)
{
isVender = false;
}
public NPC(string a_name, List<string> images, bool a_bool) : base(a_name, images)
{
isVender = a_bool;
}
}
is there a way to get acces directly to parent's attributes in C# just like Eiffel?
如果您没有为 class 成员指定可访问性,默认情况下它们是 private
,这意味着它们只能在 class 本身内访问。所以 最简单的 改变就是让它们 protected
.
public class Character
{
protected Int32 Id;
protected String name;
protected List<String> images;
更好 的解决方案可能是在字段顶部添加 protected
属性,这仍然会保护实际字段但允许继承 classes更改属性的行为。
这可能是一个愚蠢的问题,但我是 C# 的新手。我想知道有没有办法直接在childclass中使用parent的属性。我做了很多 Eiffel,当一个 class 被一个或多个 classes 继承时(因为是的,Eiffel 没有接口,你可以继承多个 classes)。
就像那个例子:(埃菲尔语言)
Parent Class:
class Parent
features
int id
string lastName
Child Class:
class Child inherit
PARENT
feature
bool isChild
在那种情况下,Child class 已经可以访问 id 和 lastName 并且可以直接设置为 Child 属性的一部分,不必创建 Parent.
但到目前为止我做了这个(C# 语言):
Parent class:
public class Character
{
Int32 Id;
String name;
List<String> images;
public Character()
{
name = "";
images = null;
}
public Character(string a_name, List<String> imagePaths)
{
name = a_name;
images = imagePaths;
}
public Character(Int32 a_id, string a_name, List<String> imagePaths)
{
Id = a_id;
name = a_name;
images = imagePaths;
}
}
Child class:
public class NPC : Character
{
public bool isVender;
public NPC()
{
Character character = new Character();
isVender = false;
}
public NPC(string a_name, List<String> images)
{
Character caracter = new Character(a_name, images);
isVender = false;
}
public NPC(string a_name, List<string> images, bool a_bool)
{
Character caracter = new Character(a_name, images);
isVender = a_bool;
}
}
所以我的问题是,有没有办法像 Eiffel 一样在 C# 中直接访问 parent 的属性?
将要在子 类 中使用的字段声明为 protected
。详细了解受保护的可见性修饰符 here.
public class Character
{
protected Int32 Id;
protected String name;
protected List<String> images;
public Character()
{
name = "";
images = null;
}
public Character(string a_name, List<String> imagePaths)
{
name = a_name;
images = imagePaths;
}
public Character(Int32 a_id, string a_name, List<String> imagePaths)
{
Id = a_id;
name = a_name;
images = imagePaths;
}
}
然后你就可以在子类.
中使用受保护的字段了你真正似乎想要的是使用基础class的构造函数来设置基础class的字段,这是一个C# 中的语法略有不同:
public class NPC : Character
{
public bool isVender;
public NPC() : base()
{
isVender = false;
}
public NPC(string a_name, List<String> images) : base(a_name, images)
{
isVender = false;
}
public NPC(string a_name, List<string> images, bool a_bool) : base(a_name, images)
{
isVender = a_bool;
}
}
is there a way to get acces directly to parent's attributes in C# just like Eiffel?
如果您没有为 class 成员指定可访问性,默认情况下它们是 private
,这意味着它们只能在 class 本身内访问。所以 最简单的 改变就是让它们 protected
.
public class Character
{
protected Int32 Id;
protected String name;
protected List<String> images;
更好 的解决方案可能是在字段顶部添加 protected
属性,这仍然会保护实际字段但允许继承 classes更改属性的行为。