如何在课堂上使用抽象成员 [英] how to use abstract members in class

查看:67
本文介绍了如何在课堂上使用抽象成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近学习过抽象类,先生给了我一个很好的解释.
外植就是这样
1)使用诸如age,名称dateofjoin等字段以及...的一些方法来实现一个抽象的人员类...
2)如果员工类别是从人继承的,他将继承全部功能
3)我们创建了经理类,它继承了人的某些特征,因为员工的角色与经理的角色不同,因此他可以使用新方法,例如裁员,添加新员工...


我的问题是如何在派生类中使用抽象成员.能否为我提供上述情况下的代码形式示例.
感谢

hi ,i recently studied abstract classes,my sir gave me an wonderful explanation.
the explantion goes in this way
1)implement a abstract person class with fields like age ,name dateofjoin,...and some methods like experience...
2)if employee class inherits from person ,he will inherit the total functionalities
3)we create manager class ,it inherits some features from person ,since role of employee is diiferent from manager he can have new methods like fireemployee ,add new employee...


my question is how to use the abstract members in derived classes .can u give me example in form of code for the above scenario.
thanks

推荐答案

您可以访问父类中定义的任何未定义为private的属性.

You can access any properties defined in the parent class not defined as private.

abstract class AbstractClass
{
    private int privateVariable { get; set; }
    public int publicVariable { get; set; }
    internal int internalVariable { get; set; }
    protected int protectedVariable { get; set; }
    protected internal int protectedinternalVariable { get; set; }
}

class DerivedClass : AbstractClass
{
    public DerivedClass()
    {
        // Cannot access a variable classified as private:
        //privateVariable = 0;
        // But these are valid:
        publicVariable = 1;
        internalVariable = 2;
        protectedVariable = 3;
        protectedinternalVariable = 4;
    }
}



抽象类和基类中涉及的不仅仅是访问.您可以将抽象类中的方法定义为抽象,然后派生类必须定义这些方法.您也可以在派生类中重写或创建新方法.需要查看有关抽象类的一般文章,其中包含一些信息:有关抽象类的所有信息. [ ^ ]



There is a lot more involved in abstract and base classes than just access. You can define methods in an abstract class as abstract, and then the derived class must define those methods. Also you can override or create new methods in the derived class. Need to look at general article on abstract classes, this has some of the information: All about abstract classes.[^]


尝试:
public abstract class myBase
    {
    public int MyProperty { get; set; }
    public int MyMethod(int i) { return i * MyProperty; }
    }
public class myDerived : myBase
    {
    public myDerived()
        {
        MyProperty = 14;
        Console.WriteLine(MyMethod(2));
        }
    }


这篇关于如何在课堂上使用抽象成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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