各位兄弟:
今在公司看java编程思想 第三版,遇到一个习题,有知道做法的请写下你的代码:(谢谢)Write a class with a method boolean print(int) that prints a value and returns a boolean. Now overload the method to return a long.
今在公司看java编程思想 第三版,遇到一个习题,有知道做法的请写下你的代码:(谢谢)Write a class with a method boolean print(int) that prints a value and returns a boolean. Now overload the method to return a long.
编译器会报错。
返回变成long了怎么能叫overload呢~?
不明白
创建一个Dog类,让其重载bark()方法,此方法应该根据不同的数据类型进行重载,并调用版本的不同,打印出是
barking 还是 howling
class Dog {
public void bark() {
System.out.println("Default bark!");
}
public void bark(int i) {
System.out.println("int bark = howl");
}
public void bark(double f) {
System.out.println("float bark = yip");
}
// Etc. ...
}public class E06_OverloadedDog {
public static void main(String args[]) {
Dog dog = new Dog();
dog.bark();
dog.bark(1);
dog.bark(1.1);
}
} ///:~
Additional Exercise: (This is a trick question, so watch out). Write a class with a method boolean print(int) that prints a value and returns a boolean. Now overload the method to return a long. (Note: this is similar to some kinds of questions on the Sun Java Certification Exam).
英文部分是作者自己加的一个练习,请大家看一下怎么这个类
喜题解答.你去下吧.不要积分的
public boolean print(int i)
{
System.out.println("print i");
return false;
}
public long print(long i)
{
return i;
}
}