【发布时间】:2013-12-02 02:01:10
【问题描述】:
有人可以向我解释以下问题吗?我不是要你为我回答,而是要你向我解释,这样我才能朝着正确的方向前进。我一直在这门课上苦苦挣扎……我自学了html和css,可以像手背一样流利地写出来。所以当我报名参加这门课时,我想我也会很容易地学习 java... 伙计,我错了:)
public class TestGlass
{
public static void main(String [] args)
{
Glass milk = new Glass(15); // 15 ounces of milk
Glass juice = new Glass(3); // 3 ources of juice
milk.drink(2);
milk.drink(1);
milk.report();
juice.fill(6); // went from 3 to 9 ounces
juice.drink(1); // now down to 8 ounces
juice.report();
juice.spill();
juice.report();
}
}
class Glass
{
// Declare a variable to keep track of the number of ounces in a glass here
// Write the methods here. You will need a constructor, plus the drink, report, fill,
// and spill methods. These methods are for any generic glass -- they are not specific
// to the milk or juice instances of a glass (i.e., you should not mention "juice" or
// "milk" inside the Glass class).
}
这就是输出的样子......
玻璃有 12 盎司。
玻璃杯有 8 盎司。
玻璃有 0 盎司。
【问题讨论】:
-
您究竟需要澄清什么?
-
几乎任何基本的 Java 教程都会向您展示构造函数和方法的示例。我建议你使用谷歌。
标签: java methods constructor