练习1:笔试&面试
题目1:继承成员变量和继承方法的区别
class Base {
int count = 10;
public void display() {
System.out.println(this.count);
}
}class Sub extends Base {
int count = 20;
public void display() {
System.out.println(this.count);
}
}public class FieldMethodTest {
public static void main(String[] args){
Sub s = new Sub();
System.out.println(s.count);
s.display();
Base b = s;
System.out.println(b == s);
System.out.println(b.count);
b.display();
}
}
题目2:
//考查多态的笔试题目:
public class InterviewTest1 {public static void main(String[] args) {
Base base = new Sub();
base.add(1, 2, 3);// Sub s = (Sub)base;
// s.add(1,2,3);
}
}class Base {
public void add(int a, int... arr) {
System.out.println("base");
}
}class Sub extends Base {
public void add(int a, int[] arr) {
System.out.println("sub_1");
}// public void add(int a, int b, int c) {
// System.out.println("sub_2");
// }}
题目3:
//getXxx()和setXxx()声明在哪个类中,内部操作的属性就是哪个类里的。
public class InterviewTest2 {
public static void main(String[] args) {
Father f = new Father();
Son s = new Son();
System.out.println(f.getInfo());//atguigu
System.out.println(s.getInfo());//尚硅谷
s.test();//尚硅谷 atguigu
System.out.println("-----------------");
s.setInfo("大硅谷");
System.out.println(f.getInfo());//atguigu
System.out.println(s.getInfo());//大硅谷
s.test();//大硅谷 atguigu
}
}class Father {
private String info = "atguigu";public void setInfo(String info) {
this.info = info;
}public String getInfo() {
return info;
}
}class Son extends Father {
private String info = "尚硅谷";
public void setInfo(String info) {
this.info = info;
}public String getInfo() {
return info;
}
public void test() {
System.out.println(this.getInfo());
System.out.println(super.getInfo());
}
}
题目4:多态是编译时行为还是运行时行为?
//证明如下:
class Animal {
protected void eat() {
System.out.println("animal eat food");
}
}class Cat extends Animal {
protected void eat() {
System.out.println("cat eat fish");
}
}class Dog extends Animal {
public void eat() {
System.out.println("Dog eat bone");
}
}class Sheep extends Animal {
public void eat() {
System.out.println("Sheep eat grass");
}
}public class InterviewTest {
public static Animal getInstance(int key) {
switch (key) {
case 0:
return new Cat ();
case 1:
return new Dog ();
default:
return new Sheep ();
}}
public static void main(String[] args) {
int key = new Random().nextInt(3);
System.out.println(key);Animal animal = getInstance(key);
animal.eat();
}
}
练习2:
class Person {
protected String name="person";
protected int age=50;
public String getInfo() {
return "Name: "+ name + "\n" +"age: "+ age;
}
}
class Student extends Person {
protected String school="pku";
public String getInfo() {
return "Name: "+ name + "\nage: "+ age
+ "\nschool: "+ school;
}
}
class Graduate extends Student{
public String major="IT";
public String getInfo()
{
return "Name: "+ name + "\nage: "+ age
+ "\nschool: "+ school+"\nmajor:"+major;
}
}
建立InstanceTest 类,在类中定义方法method(Person e); 在method中: (1)根据e的类型调用相应类的getInfo()方法。 (2)根据e的类型执行: 如果e为Person类的对象,输出: “a person”; 如果e为Student类的对象,输出: “a student” “a person ” 如果e为Graduate类的对象,输出: “a graduated student” “a student” “a person”
解题:
package chapter07_oop2_teacher.src.com.atguigu06.polymorphism.exer3;/*** ClassName: InstanceTest* Description:* 建立InstanceTest 类,在类中定义方法method(Person e);* 在method中:* (1)根据e的类型调用相应类的getInfo()方法。* (2)根据e的类型执行:* 如果e为Person类的对象,输出:* “a person”;* 如果e为Student类的对象,输出:* “a student”* “a person ”* 如果e为Graduate类的对象,输出:* “a graduated student”* “a student”* “a person”* @Author 尚硅谷-宋红康* @Create 9:13* @Version 1.0*/ public class InstanceTest {public static void main(String[] args) {InstanceTest test = new InstanceTest();test.method(new Student());}public void method(Person e){System.out.println(e.getInfo());//方式1: // if(e instanceof Graduate){ // System.out.println("a graduated student"); // System.out.println("a student"); // System.out.println("a person"); // }else if(e instanceof Student){ // System.out.println("a student"); // System.out.println("a person"); // }else{ // System.out.println("a person"); // }//方式2if(e instanceof Graduate){System.out.println("a graduated student");}if(e instanceof Student){System.out.println("a student");}if(e instanceof Person){System.out.println("a person");}} }
package chapter07_oop2_teacher.src.com.atguigu06.polymorphism.exer3;/*** ClassName: Graduate* Description:** @Author 尚硅谷-宋红康* @Create 9:15* @Version 1.0*/ public class Graduate extends Student{public String major="IT";public String getInfo(){return "Name: "+ name + "\nage: "+ age+ "\nschool: "+ school+"\nmajor:"+major;} }
package chapter07_oop2_teacher.src.com.atguigu06.polymorphism.exer3;/*** ClassName: Person* Description:** @Author 尚硅谷-宋红康* @Create 9:14* @Version 1.0*/ public class Person {protected String name="person";protected int age=50;public String getInfo() {return "Name: "+ name + "\n" +"age: "+ age;} }
package chapter07_oop2_teacher.src.com.atguigu06.polymorphism.exer3;/*** ClassName: Student* Description:** @Author 尚硅谷-宋红康* @Create 9:14* @Version 1.0*/ public class Student extends Person {protected String school="pku";public String getInfo() {return "Name: "+ name + "\nage: "+ age+ "\nschool: "+ school;} }