简体   繁体   English

返回和抽象类?

[英]Returning and Abstract Class?

I have an Abstract class that is like: 我有一个Abstract类,如:

public abstract class myClass{    

    public abstract double methodOne();
    // returns the data member value for standard deviation

    public abstract double methodTwo(String myString, int myInt);
    // returns the data member value for mean percentage

}

Then I have a class which extends this: 然后,我有一个扩展此的类:

public class Abstract extends myClass{

    public double methodOne() {
        return 0.00;
    }

    public double methodTwo(String myString, int myInt) {
        return 20.00;   
    }    
}

Then finally I have a normal method that's return type is myClass. 最后,我有一个返回类型为myClass的普通方法。

public myClass anotherMethodOfMine(String myString, int myInt){

}

A simple question really, I just don't understand how a method can return another class that itself contains methods? 确实,这是一个简单的问题,我只是不理解方法如何返回另一个本身包含方法的类? Could someone explain it to me? 有人可以向我解释吗?

The method returns an object of MyClass on which you can call the methods defined there. 该方法返回MyClass对象,您可以在该对象上调用在那里定义的方法。

You should understand that a class is just a definition of behaviour and data. 您应该了解,类只是行为和数据的定义。 An object is the actual entity that can perform things with data and can invoke the behaviours defined in the class. 对象是实际的实体,可以执行数据操作并可以调用类中定义的行为。

Even though your myClass can not be instantiated directly (because it's an abstract class) Abstract is a subclass of myClass ; 即使您的myClass不能直接实例化(因为它是一个抽象类), AbstractmyClass的子类; this means it has a is a relationship. 这意味着它具有一个关系。

Because of this, Abstract "is a" myClass and it can be returned as an instantiation of the parent class ( myClass ). 因此, Abstract “是一个” myClass ,它可以作为父类( myClass )的实例返回。

Your Method anotherMethodOfMine(String myString, int myInt) which will inturn have a object instantiated and returned to the calling function. 您的方法anotherMethodOfMine(String myString, int myInt)将实例化一个对象并返回给调用函数。 where you can access the myclass object and use the methods which are defined in the myclass. 您可以在其中访问myclass对象并使用myclass中定义的方法。

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

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