English 中文(简体)
简单的 Java方案一需要帮助
原标题:A simple Java program I need help with
  • 时间:2011-07-26 17:53:06
  •  标签:
  • java

我必须完成 Java方案,他说:

撰写“java”方案,以实施继承。 您的方案应有以下结构:

  1. Create a Base class to hold two Integers and a method to Display them.
  2. In the derived Class add another Integer and Display it.
  3. Create a method in the same derived Class to add the three numbers.
  4. Pass the values to the integers and Display the results.

我知道,在 Java,我不知道我做什么是正确还是错的,还是什么! 我认为,我不理解所希望的东西。 这里我要谈谈:

public class firstclass {
    int a=5;
    int b=6;

    public void Display (){
        System.out.println(a+b);
    }
}

public class secondclass extends firstclass {
    int z=0;

    public void Displaysecond (){
        System.out.println(z);
    }

    public void add (){
        z=a+b;
        System.out.println(z);
    }
}

public class mainOne {
    public static void main(String[] args) {
        firstclass call = new firstclass();
        secondclass call2 = new secondclass();

        call.Display();
        call2.Displaysecond();
        call2.add();
    }
}

It runs without any problems but I get "11" for the "System.out.println(a+b);" while a = 5 and b = 6.
Am I going about this correctly?

最佳回答

你有几个问题。 正如格拉米特提到的那样,你的附加功能不是你的老师想要的。 你们的老师们想给你们一个、b和z。 So

public void add(){
    int sum = a + b + z;
    System.out.println(sum);
}

或者,请在<条码>中添加(<>/条码>和<>条码>。

第二,你没有遵守 Java公约。 您的变量需要更不恰当地标明,你的类别需要从上层开始,你们的方法需要从下级开始。

或许,你应该把第一部落、第二部落和主要部族的名称命名?

Your methods: display(), displaySecond(), and add()

页: 1 (一致性)

此外,电话: 显示()应当产生与电话2相同的结果,因为二类激光器是一类激光器。 http://www.javabeginner.com/learn-java/java-inheritance” rel=“nofollow”>this,用于更好地了解遗产。

A side note: you should add your numbers in parentheses () for clarification and safety if you re doing some sort of in-line math. For example

public void display(){
    System.out.println((a + b));
}

instead of

public void display(){
    System.out.println(a+b);
}
问题回答

除非:

3. Create a method in the same derived Class to add the three numbers.

public void add ()
{
    z=a+b;
    System.out.println(z);
}

这种方法增加了一个和 b,但随后将其置于z。 而它却不归还任何东西,因此它应该像:

public int add ()
{
    return a+b+z;
}

之后,在你们的主人中,应该有:

System.out.println(call2.add());




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签