Java third time job

Source: Internet
Author: User

(a) Learning summary

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 be compiled, super calls the parent class constructor, calls the parent class constructor to write in the first
How to modify: Write super to the first sentence
Operation Result:
Grandparent Created.String:Hello.Grandparent.
Parent Created
Child Created
Can't turn
Because the data subclasses in the parent class can be obtained directly. So when you create a subclass object, you need to see how the parent class initializes the data.
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();    }}

No method defined in Animal class sleep ()
For a downward transformation, a forced transformation is required, that is, the subclass type to be transformed must be clearly specified: format: Subclass Name Subclass Object = (subclass) Parent class instance;
Correct:

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 = (Dog) animal;        dog.sleep();         Animal animal2 = new Animal();        if(animal2 instanceof Dog)        {            dog = (Dog)animal2;            dog.shout();        }    }}

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]

Description: System.out.println (per); The ToString method for calling the parent class object by default
(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?
Returns null if the argument is an empty string, otherwise returns the return value of ToString ().
ToString () returns a string that describes the specific contents of the current object: the class name @ object's hash code hexadecimal representation
(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?
Refer to the textbook P229

Execution Result:
Name: Zhang San, age: 20
Name: Zhang San, age: 20
Description The override of the ToString class of the parent class object was completed in the person class

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 the taxi abstract class, which contains the number, name, rent three basic attributes
Define passenger cars, trucks, pickups three classes inherit the taxi abstract class, then define their own properties separately

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();        }    }

No
All methods in the interface must be overridden in the dog class, plus the Void Run () method in the Dog class

(ii) Experimental summary
With the Java learning time is getting longer and more familiar with Java, but also understand a number of object-oriented, in general, the problems encountered in most of them can solve
At first, the upward transformation and the downward transformation do not understand, and later read more reading the textbook will understand
(c) Code hosting

Https://gitee.com/hebau_java_cs16/Java_CS01JG/tree/master

Java third time job

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.