Java Inheritance - cannot find method to invoke on object?

爱⌒轻易说出口 提交于 2019-12-13 21:47:02

问题


To begin with I have three classes. One called:

  1. Temperature (superclass)

  2. Weather (subclass superclass)

  3. UseTemperature (subclass that holds main method)

For this program the main method is only supposed to display the temperature in Celsius and wind speed. I do not know what the issue is though..

My issue is that setWindSpeed/getWindSpeed cannot be found within UseTemperature.

//TEMPERATURE:

public class Temperature
{
    private double degrees;


    public void setDegrees (double degrees)
    {
        this.degrees = degrees;
    }


    public double getDegrees ()
    {
        return degrees;
    }

/////////////////////////////////////////////////////////////////////////////////////////
//WEATHER:

public class Weather extends Temperature
{
    private double windspeed; // Number + km/h

    private void setWindSpeed (double windspeed) //setter
    {
        this.windspeed = windspeed;
    }


    public double getWindSpeed ()  //getter
    {
        return windspeed;
    }
}
/////////////////////////////////////////////////////////////////////////////////////////
//USE TEMPERATURE:

class UseTemperature // can be public but makes no diffrence
{
    public static void main (String args[])
    {
        Temperature temp;

        temp = new Temperature ();

        temp.setDegrees (40.0);      
        temp.setWindSpeed (70.0); // NOT FOUND IN TEMPERATURE

        System.out.print (temp.getDegrees ());
        System.out.print (" degrees ");

        System.out.println (temp.getWindSpeed()); // NOT FOUND IN TEMPERATURE
        System.out.println (" km/h");

    }
}

回答1:


The class Temperature does not contain the methods setWindSpeed or getWindSpeed. You would need to create a Weather object and make those methods accessible to call them.

Weather myWeather = new Weather();
// set/get ...



回答2:


Either you need to declare setWindSpeed/getWindSpeed in tempaerature class or do

Weather temp = new Weather ();

the reason is when you do

Temperature temp;
 temp = new Temperature ();
 temp.getWindSpeed()

compiler just see whether getWindSpeed lies inside Temperature class or not. It does not. So it gives compilation error




回答3:


Temperature class indeed does not have getWindSpeed and setWindSpeed methods. It is only the subclass Weather which has got those methods. Superclasses know nothing about Subclasses. Instead of creating an instance of Temperature, create that of Weather




回答4:


UseTemperature is not actually extending Weather, and anyway it would not be able to access the (private) setWindSpeed method. You could access it if you were to make it a nested class (see http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html for details), but it is probably not a good design option.

Instead, if UseTemperature is meant to be your application access point and never to be instantiated (like in the snippet you posted), consider changing the access modifier of setWindSpeed to something a bit more permissive, like protected or even default. That would allow you to access it from a class in the same package without having to subclass it.

Edit: also, as Reimeus pointed out, a Weather object must be instantiated instead of a Temperature one.




回答5:


How can you access setWindSpeed and getWindSpeed from Temperature. Remember all subclass are superclass but all superclass are not subclass. You need to define setWindSpeed and getWindSpeed in Temperature.

After defining access getWindSpeed and setWindSpeed like this.

Temperature t = new Weather();

t.setWindSpeed(); and so on.

Also don't make those method private in Weather. You can access them only when you make them public or atleast protected in Temperature.



来源:https://stackoverflow.com/questions/14790445/java-inheritance-cannot-find-method-to-invoke-on-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!