1. Read the procedure below to see if you can compile the pass? If not, explain why. How should I modify it? What is the result of running the program? Why do I have to call the construction method of the parent class before the constructor of the subclass is run? Can you turn around?
class Grandparent { public Grandparent() { System.out.println("GrandParent Created."); } public Grandparent(String string) { System.out.println("GrandParent Created.String:" + string); }}class Parent extends Grandparent { public Parent() { System.out.println("Parent Created"); super("Hello.Grandparent."); }}class Child extends Parent { public Child() { System.out.println("Child Created"); }}public class Test{ public static void main(String args[]) { Child c = new Child(); }}
Cannot pass, super ("Hello.grandparent.") inside the parent class Don't put it on the first line, just put it on the first line.
Result is
Grandparent Created.String:Hello.Grandparent.
Parent Created
Child Created
Because the construction method of the parent class is not applied in the subclass, it is equivalent to no initialization, error, and cannot be reversed.
2. Read the procedure below, analyze what errors exist in the program, explain why, and how to correct them? What is the result of running the correct program?
class Animal{ void shout(){ System.out.println("动物叫!"); }}class Dog extends Animal{ public void shout(){ System.out.println("汪汪......!"); } public void sleep() { System.out.println("狗狗睡觉......"); } }public class Test{ public static void main(String args[]) { Animal animal = new Dog(); animal.shout(); animal.sleep(); Dog dog = animal; dog.sleep(); Animal animal2 = new Animal(); dog = (Dog)animal2; dog.shout(); }}
Animal.sleep () and Dog dog=animal wrong
Animal.sleep () is because the sleep method in the dog class after the transformation is not inherited or rewritten, so there is a mistake, delete this method is OK.
Dog Dog=animal is because the type conversion should be forced in the downward transition and should be dog dog= (dog) Animal
3. Run the following procedure
class Person { private String name ; private int age ; public Person(String name,int age){ this.name = name ; this.age = age ; } }public class Test{ public static void main(String args[]){ Person per = new Person("张三",20) ; System.out.println(per); System.out.println(per.toString()) ; } }
(1) The operation result of the program is as follows, what is the problem?
[Email protected]
[Email protected]
The person class does not write the ToString method, and the test class is called directly.
(2) So, what is the result of the program running? Use Eclipse to open the source code of the println (per) method and see which methods are called in the method, can you explain the results of this example?
(3) Add the following method to the person class
public String toString(){ return "姓名:" + this.name + ",年龄:" + this.age ;
Rerun the program, what is the execution result of the program? What's the problem?
Program execution Results:
Name: Zhang San, age: 20
Name: Zhang San, age: 20
The newly added ToString method override the method of the person class inheritance, outputting the newly added tostring method.
4. Car rental companies, taxi types have passenger cars, trucks and pickup three kinds, each car in addition to have a number, name, rent three basic attributes, passenger cars have capacity, goods vehicles have cargo capacity, pickup is both carrying capacity and cargo capacity. This paper analyzes the above problems with object-oriented programming, and represents them as appropriate classes, abstract classes or interfaces, and illustrates the design ideas. Now to create a list of rental cars, how should I create them?
Define a taxi class, which has a number, name, rent three basic attributes, write an abstract method. The bus class inherits the taxi class, realizes the abstract method output carrying capacity, defines the freight car class inherits the taxi class, realizes the abstract method output cargo capacity, the definition pickup class inherits the taxi class, realizes the abstract method output carrying capacity and the cargo capacity. Creates an array of objects for a taxi class, and then initializes it.
5. Read the procedure below to analyze if the code can be compiled, if not, explain why, and make corrections. If you can, list the results of the operation
interface Animal{ void breathe(); void run(); void eat(); } class Dog implements Animal{ public void breathe(){ System.out.println("I‘m breathing"); } void eat(){ System.out.println("I‘m eating"); } } public class Test{ public static void main(String[] args){ Dog dog = new Dog(); dog.breathe(); dog.eat(); } }
Can not compile through, 1.Dog class inside the Eat method should have public
The 2.Dog class should overwrite all methods of the parent class, and the run () method is not covered.
(ii) Experimental summary
When sorting, you can compare them with gettime.
(c) Code hosting
https://gitee.com/deep_tenderness/hebau_cs01WSS/tree/master/ex04
Java third time job