小心下面两个程序的输出结果
2005-11-02 09:29
302 查看
对初学者来说,两个比较好的例子,写出J***A下的输出结果
1)
public class Test {
public static void main(String[] args) {
Child child = new Child();
}
}
class Parent {
Parent() {
System.out.println("to construct Parent.");
}
}
class Child extends Parent {
Child() {
System.out.println("to construct Child.");
}
Delegatee delegatee = new Delegatee();
}
class Delegatee {
Delegatee() {
System.out.println("to construct Delegatee.");
}
}
2)
class Base {
Base() {
System.out.println("Base() before print()");
print();
System.out.println("Base() after print()");
}
public void print() {
System.out.println("Base.print()");
}
}
class Derived extends Base {
int value;
Derived(int val) {
value = val;
System.out.println("Derived() with " + value);
}
public void print() {
System.out.println("Derived.print() with " + value);
}
}
public class Polymorphism {
public static void main(String[] args) {
new Derived(123);
}
}
1)
public class Test {
public static void main(String[] args) {
Child child = new Child();
}
}
class Parent {
Parent() {
System.out.println("to construct Parent.");
}
}
class Child extends Parent {
Child() {
System.out.println("to construct Child.");
}
Delegatee delegatee = new Delegatee();
}
class Delegatee {
Delegatee() {
System.out.println("to construct Delegatee.");
}
}
2)
class Base {
Base() {
System.out.println("Base() before print()");
print();
System.out.println("Base() after print()");
}
public void print() {
System.out.println("Base.print()");
}
}
class Derived extends Base {
int value;
Derived(int val) {
value = val;
System.out.println("Derived() with " + value);
}
public void print() {
System.out.println("Derived.print() with " + value);
}
}
public class Polymorphism {
public static void main(String[] args) {
new Derived(123);
}
}
相关文章推荐
- 小心下面两个程序的输出结果
- 第13周-阅读项目4-写出下面程序的输出结果
- 写一个程序,接收两个整数a和b,并判断a是否能被b整除,输出判断结果。
- 输出下面程序运行结果
- Chapter9——分析下面的程序,写出其运行时的输出结果/将程序第5行改为Date(int=1,int=1,int=2005);修改程序使之能通过编译
- 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序
- 下面程序的输出结果是多少?
- 下面的程序代码输出的结果是多少?
- 第十六周项目3:查看下面程序的输出结果,并解释为什么
- 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序
- 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)
- 整除运算, 写一个程序,接收两个整数a和b,关判断a是否能被b整除,输出判断结果
- (本程序功能:字符串A中找出包含字符串B中所有字符的位置并输出) 在使用全局变量m的时候,输出错误的结果,而用下面局部变量M就没问题。
- 请各位帮忙看一下,第一个程序为什么不能输出和第二个程序一样的结果?这两个程序只有求最大公约束的代码不同
- java基础问题---------下面的程序代码输出的结果是多少
- 2、编写程序,从键盘上输入两个电阻的值,求它们并联和串连的电阻值,输出结果保留两位小数。
- 转:整除运算, 写一个程序,接收两个整数a和b,关判断a是否能被b整除,输出判断结果
- 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序
- Java-第十四章-带参的方法(二)-编写程序,判断是否为素数.并根据判断结果输出语句
- 【华为机试题】请在123456789之间添加+号或者-号,也可以什么都不填,使得等式的运算结果等于a。要求程序输出等式成立的个数b。测试用例:a=5时,b=21。