如何将类声明为另一个类的特征 [英] How do I declare a class as a characteristics into another class

查看:65
本文介绍了如何将类声明为另一个类的特征的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将一个类作为特征声明到另一个类中



我想做一个项目



我尝试了什么:



我不知道怎么做,请帮助我...

How do i declare a class as a characteristics into another class

I want to make a project

What I have tried:

I dont know how to do that,please help me in this...

推荐答案

我不确定你的意思是将一个类声明为另一个类的特征 - 这不是我认识的技术描述。



我能想到的最好的就是你想从一个类中 继承 ,从而获得对它的访问权限,属性和方法。这很容易做到:

I'm not sure at all what you mean by "declare a class as a characteristics into another class" - it's not a technical description that I recognise.

The best I can come up with is that you want to inherit from a class, and thus get access to it's fields, properties, and methods. That's easy enough to do:
public class A
   {
   protected int A1 = 666;
   public int A2 {get; set};
   public void PrintMe()
      {
      Console.WriteLine("{0}:{1}", A1, A2);
      }
   }
public class B : A
   {
   public void DoSomething()
      {
      PrintMe();
      Console.WriteLine(A1 * 100);
      }
   }
...
   A a = new A();
   B b = new B();
   a.PrintMe();
   b.A2 += 10;
   b.PrintMe();
   b.DoSomething();
...

B继承自A(应该通过类声明行中的:A),因此它获取所有A字段,属性和方法。

B inherits from A (as should by the ": A" on the class declaration line) so it acquires all the A fields, properties, and methods.


这篇关于如何将类声明为另一个类的特征的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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