import java.io.*; class Person{ public void print(){System.out.print("Person ");} public void printMyGender(String s){ this.print(); System.out.print(s+" "); } } class Gender{ String type="gender"; public void print(Person p){p.printMyGender(type);} } class Female extends Gender{ public Female(){ type="female"; } } class Male extends Gender{ public Male(){ type="male"; } } class Employee extends Person{ public void print(){ System.out.print("Employee ");} } class Manager extends Employee{ public void print(){ System.out.print("Manager ");} } public class Test{ public static void main(String[] args){ Manager man = new Manager(); Employee em = new Employee(); Gender gender1 = new Male(); Gender gender2 = new Female(); gender1.print(man); gender2.print(em); } } 对于以上代码,其运行结果是
举一反三
- import java.io.*;</p></p> class Person{</p></p> public void print(){System.out.print("Person ");}</p></p> public void printMyGender(String s){</p></p> this.print();</p></p> System.out.print(s+" ");</p></p> }</p></p> }</p></p> class Gender{</p></p> String type="gender";</p></p> public void print(Person p){p.printMyGender(type);}</p></p> }</p></p> class Female extends Gender{</p></p> public Female(){</p></p> type="female";</p></p> }</p></p> }</p></p> class Male extends Gender{</p></p> public Male(){</p></p> type="male";</p></p> }</p></p> }</p></p> class Employee extends Person{</p></p> public void print(){</p></p> System.out.print("Employee ");}</p></p> }</p></p> class Manager extends Employee{</p></p> public void print(){</p></p> System.out.print("Manager ");}</p></p> }</p></p> public class Test{</p></p> public static void main(String[] args){</p></p> Manager man = new Manager();</p></p> Employee em = new Employee();</p></p> Gender gender1 = new Male();</p></p> Gender gender2 = new Female();</p></p> gender1.print(man);</p></p> gender2.print(em);</p></p> }</p></p> }</p></p> 对于以上代码,其运行结果是</p></p>
- 已知</p></p><p><p> import java.io.*;</p></p><p><p>class Person{</p></p><p><p>public static void print(){System.out.print("Person");}</p></p><p><p>}</p></p><p><p> class Employee extends Person{</p></p><p><p> public void print(){</p></p><p><p> System.out.print("Employee");}</p></p><p><p>}</p></p><p><p>class Manager extends Employee{</p></p><p><p> public void print(){</p></p><p><p> System.out.print("Manager");}</p></p><p><p>}</p></p><p><p>public class Test{</p></p><p><p> public static void main(String[] args){</p></p><p><p> Manager man = new Manager();</p></p><p><p> Employee emp1 = new Employee();</p></p><p><p> Employee emp2 = (Employee)man;</p></p><p><p> Person person = (Person)man;</p></p><p><p> emp2.print();</p></p><p><p> System.out.print("#");</p></p><p><p> person.print();}</p></p><p><p>}</p></p><p><p>对于以上代码,其输出结果是</p></p>
- Java程序如下 class A{ public A(){System.out.print("A");} } class B extends A{ public B(){System.out.print("B");} public static void main(String[]s){ new B(); } } 该程序将( )。
- (7-1)以下程序的运行结果是( )。 class A{ A(){ System.out.print(10); } } public class Demo extends A { public static void main(String[] args) { new A(); new Demo(); } }
- 以下代码执行后的结果是: public class Person { String name = “小芳”; public Person(String name) { name = “小兰”; } public void show() { this.name = “小翠”; } public static void main(String[] args) { Person p = new Person(“小凤”); System.out.print(p.name); p.show(); System.out.print(p.name); } }