"Java Technology" 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();    }}
    不能通过编译,super语句只能写在子类构造方法的首行,运行结果:
GrandParent Created.String:Hello.Grandparent.Parent CreatedChild Created
    子类的构造方法在运行之前,必须调用父 类的构造方法,因为在类中的所有方法中,只有构造方法是被优先调用的,所以所以super调用构造方法必须只能放在构造方法的首行,并且在一个构造方法中只能出现一次。所以不能反过来。

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();错误,父类对象不能调用子类新增加的方法,调用只能调用子类继承和重写过的方法;Dog dog = animal;错误,在进行对象的向下转型前,必须首先发生对象的向上转型。修改:
class Animal{  void shout(){      System.out.println("动物叫!");  }  public void sleep(){};}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();        dog = (Dog)animal2;        dog.shout();    }}

Operation Result:

汪汪......!狗狗睡觉......狗狗睡觉......

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]
Because the ToString method is not overridden in the person class, the test class is actually called the ToString method in the object class.
(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 a string that describes the current object, returning the exact content: the class name @ object's hash code hexadecimal representation.
Source:

(3) Add the following method to the person class

  public String toString () {return "name:" + THIS.name + ", Age:" + this.age; } ' rerun the program, what is the execution result of the program? What's the problem? Running Result:! [] (Https://images2018.cnblogs.com/blog/1349377/201804/1349377-20180418213203496-319720219.png) The object class is the parent class of all classes, So the output object is called by the Quilt class overridden by the ToString method, even if the object is not written after. ToString, the default invocation is also the subclass overridden by the ToString method, so the two output is the same. can refer to the textbook P2294. 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 properties, passenger cars have capacity, goods vehicles have cargo capacity, pickup is at the same time 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? Create a Car interface: Includes number, name, rent three properties, a capacity interface, a cargo capacity interface, create a bus class: Inherit the car interface and the capacity of the interface; a van class: inherits the car interface and cargo capacity interface; A pickup class: inherits the car interface, the capacity interface, and the cargo capacity interface. Then create a car hire class, declare the interface variables, instantiate the corresponding subclass object, call the subclass rewrite method, for the taxi list. 5. Read the procedure below to analyze if the code can be compiled, if not, explain why, and make corrections. If available, lists the results of the run  
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();    }}
        不能通过编译,第一:因为接口是由全局变量和抽象方法组成的,访问权限必须是public,在子类覆写时不能降低访问标志符的级别,所以应改成 public void eat()        第二:子类必须要继承父类的所有方法。更改:
interface Animal{        void breathe();    void run();    void eat();}class Dog implements Animal{    public void breathe(){        System.out.println("I‘m breathing");    }   public void eat(){        System.out.println("I‘m eating");    }    public void run() {    System.out.println("I‘m runing");}}public class Test{    public static void main(String[] args){        Dog dog = new Dog();        dog.breathe();        dog.eat();    }}

```
Operation Result:

6. Other content that needs to be summarized.

(ii) Experimental summary

This experiment includes experiment four and experiment 52 times contents:
To summarize the problems encountered in the process of the completion of the experimental content, the solution and the design thinking and thinking of the procedure. The
format is as follows:
Experiment four
1, program design ideas: Define a bank class, including static method welcome language, construction method, deposit method, withdrawal method, static method concluding remarks. The trading class is used for simulation testing.
Issue 1: In the test, there is no accumulation of savings, account opening information is not stored, to withdraw money and save the account on the basis of the addition and subtraction.
Cause: When declaring an object, it is declared inside the loop, so more than one object appears
solution: In the external unified declaration, called with the same object.
2, program design ideas: Inherit all the methods of the parent class, and add new methods.
3, the program design idea: The plane graph type class is the Circle class, the triangle class, the rectangle class to inherit, respectively the three-dimensional graph type class by the cylinder class, the cone class, the ball game inheritance; declares two objects of the parent class in the test class, respectively, for invoking the corresponding subclass method, a method for judging the perimeter, The method of volume.
4, program design ideas: Animals are the lions, monkeys, pigeons inherit, feeding class, the test class to the object up to the transformation of the instance, and open the corresponding array space, to call the subclass rewrite method.
Problem 1: Transition up
Cause: No space for arrays
Solution: Open Space separately
Experiment five
1, program design ideas: Design Pet interface, cat and dog Interface, store Category: A method for the number of space, to store pets in the store ; An Add method that stores pet information for an array; The show pet method is used to output all the pets in the store; A lookup method that is searched in the array when the user enters the pet number to be purchased, and then a new array is found, which is used for the final print of the shopping list. A show class: First show the user all the pets, the user can enter the pet number and quantity to buy, then the user can choose to continue to buy, or print the list, and finally print the shopping list and price totals.
Problem 1: A null pointer exception occurred
Cause: A problem occurred while searching, can find the corresponding pet by number, but will appear in the output of the exception, no longer down, cannot print the final total,
Solution: still unresolved

2, program design ideas: interface Animal, there are two abstract methods, interface of the dog class and Cat class, the simulator class simulator, parameters can call the subclass override method, Test class.
3, program design ideas: the definition of the abstract type of transport class, transport vehicle class inheritance abstract class, positioning interface, a Phon class implementation interface, Express task class: Output transport information. Test class: The user can enter the weight and the number, then the output logistics information.
4, program design ideas: Date class According to the way the book is rewritten, but the test class do not know how to call.

(c) Code hosting (be sure to link to your project)

Code Cloud Commit History
Upload the Experiment Project code to the Code cloud, select "Statistics-submit" in the Code cloud project, set the search time period, search for this week's submission history, and.

Https://gitee.com/hebau_java_cs16/Java_CS02GX/commits/master

"Java Technology" 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.