简体   繁体   English

如何从子类对象中隐藏父类方法?

[英]How to hide the parent class method from the child class object?

I want to hide the parent class method for the the child class object. 我想hide the parent class method for the the child class object. My Circle object display two methods to me when I going to access that methods: 我的Circle对象向我显示两种方法:

在此处输入图片说明

在此处输入图片说明

Here objShape is the object of the Shape class which is the abstract class. objShape在这里是Shape类的对象,Shape类是抽象类。

I want to hide method which is display on 2nd image. 我想隐藏显示在第二张图像上的方法。

Here Circle class extends the Oval class and Oval class extends the Shape class. 在这里,Circle类扩展了Oval类,Oval类扩展了Shape类。

Shape class coding 形状类编码

abstract public class Shape
    {        
        public abstract double Area(double dNum1 = 0.0, double dNum2 = 0.0);


        public abstract double Perimeter(double dNum1 = 0.0, double dNum2 = 0.0);

    }

Oval class Coding 椭圆形编码

class Oval : Shape
    {
        public override double Area(double dNum1 = 0.0, double dNum2 = 0.0)
        {
            double dAns = Math.PI * dNum1 * dNum2;
            return dAns;
        }

        public override double Perimeter(double dNum1 = 0.0, double dNum2 = 0.0)
        {
            double dTmp1, dTmp2, dAns;

            dTmp1 = 2 * (dNum1 * dNum1);
            dTmp2 = 2 * (dNum2 * dNum2);
            dAns = (Math.PI / 2) * Math.Sqrt(dTmp1 + dTmp2);

            return dAns;
        }      
    }

Circle Class Code 圈级代码

class Circle : Oval
    {

        public double Area(double dNum1)
        {
            double dResult = base.Area(dNum1, dNum1);
            return dResult;
        }

        public double Perimeter(double dNum1)
        {
            double dAns = base.Perimeter(dNum1, dNum1);
            return dAns;
        }

    }

So, Here how can I hide the direct access of the Oval class method for the Circle class object. 因此,在这里如何隐藏对Circle类对象的Oval类方法的直接访问。 In 2nd image it display the suggestion and I can use that method, but I don't want that suggestion and I don't want use that method with Circle class object. 在第二张图片中,它显示了建议,我可以使用该方法,但是我不想要该建议,也不想将其与Circle类对象一起使用。

In 2nd image it display the suggestion and I can use that method, but I don't want that suggestion and I don't want use that method with Circle class object. 在第二张图片中,它显示了建议,我可以使用该方法,但是我不想要该建议,也不想将其与Circle类对象一起使用。

Then you shouldn't make Circle derive from Oval , basically. 然后,基本上不应该使CircleOval派生。 Don't forget that it would be perfectly acceptable to have a variable of type Oval which has a value at execution time referring to an instance of Circle : 不要忘记,拥有Oval类型的变量是完全可以接受的,该变量在执行时具有一个引用Circle实例的值:

Oval x = new Circle();
Console.WriteLine(x.Area(10, 20));

Fundamentally, any operation that makes sense for the base class should make sense for the derived class as well, in a sensible inheritance hierarchy. 从根本上讲,在合理的继承层次结构中,对基类有意义的任何操作也应对派生类也有意义。

If you want to keep your inheritance hierarchy, you should consider a redesign such that the radius etc are part of the state of the object, instead of being passed in as parameters. 如果要保留继承层次结构,则应考虑进行重新设计,以使半径等成为对象状态的一部分,而不是作为参数传递。 For example: 例如:

public abstract class Shape
{
    public abstract double Area { get; }
    public abstract double Perimeter { get; }
}

public class Oval
{
    private double Width { get; }
    private double Height { get; }

    public override double Area { get { ... } };
    public override double Perimeter { get { ... } };

    public Oval(double width, double height)
    {
        Width = width;
        Height = height;
    }
}

public class Circle
{
    public double Radius { get { return Height; } }

    public Circle(double radius) : base(radius)
    {
    }
}

如果您不希望通过圆形显示椭圆方法,只需让Circle继承形状而不是椭圆即可。

You can achieve this by changing your access specifier of Shape and Oval class methods to protected. 您可以通过将Shape和Oval类方法的访问说明更改为protected来实现。

abstract public class Shape
{        
    protected abstract double Area(double dNum1 = 0.0, double dNum2 = 0.0);


    protected abstract double Perimeter(double dNum1 = 0.0, double dNum2 = 0.0);

}

and

class Oval : Shape
    {
        protected override double Area(double dNum1 = 0.0, double dNum2 = 0.0)
        {
            double dAns = Math.PI * dNum1 * dNum2;
            return dAns;
        }

        protected override double Perimeter(double dNum1 = 0.0, double dNum2 = 0.0)
        {
            double dTmp1, dTmp2, dAns;

            dTmp1 = 2 * (dNum1 * dNum1);
            dTmp2 = 2 * (dNum2 * dNum2);
            dAns = (Math.PI / 2) * Math.Sqrt(dTmp1 + dTmp2);

            return dAns;
        }
    }

这看起来像

I think this is what you are looking for.. 我想这就是您要寻找的..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM