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

问题描述

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



我想做一个项目



我尝试了什么:



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

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...

推荐答案

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.


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

05-27 15:02
查看更多