Java Technology's third assignment-object-oriented-inheritance, abstract class, interface

Source: Internet
Author: User

I. Key points of study
READ carefully and check the relevant information, master the following content:

Mastering the concept and design of class inheritance
Mastering the principle of inheritance of construction method
Mastering method rewriting
Mastering Super Key and final keywords
Mastering the transformation of objects, understanding the realization of object polymorphism through upward transformation.
Mastering the design of abstract classes
Mastering the design of the interface
Mastering Interface Callbacks
Understanding Simple Factory Design Patterns
Understanding the differences between abstract classes and interfaces
Mastering the application of packing class
Master the comparison method of objects and the use of comparators
Learn to use Date action classes
Learn the use of anonymous internal classes
Second, the operation requirements
Publish an essay that mainly includes the following sections:

(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 constructor of the parent class, and the Super keyword must precede the child class construction method.
Operation Result:
Grandparent Created.String:Hello.Grandparent.
Parent Created
Child Created
The constructor variables and properties of the parent class are initialized first to instantiate the subclass, not vice versa.
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();    }}

You can't call up a new method defined by a subclass
Switch
Dog dog = (dog) animal;
Dog.sleep ();
Barking......!
The dog Sleeps ...
The dog Sleeps ...
Barking......!
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 address that is the destination of the output of Per and per.tostring ()
(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?
···
public void println (String x) {
Synchronized (this) {
print (x);
NewLine ();
}
}
···

(3) Add the following method to the person class

public String toString(){         return "姓名:" + this.name + ",年龄:" + this.age ;  }  ```重新运行程序,程序的执行结果是什么?说明什么问题?运行结果:姓名:张三,年龄:20姓名:张三,年龄:20说明了在没有写toSting方法时,默认的toString方法是object中的toString方法4.汽车租赁公司,出租汽车种类有客车、货车和皮卡三种,每辆汽车除了具有编号、名称、租金三个基本属性之外,客车有载客量,货车有载货量,皮卡则同时具有载客量和载货量。用面向对象编程思想分析上述问题,将其表示成合适的类、抽象类或接口,说明设计思路。现在要创建一个可租车列表,应当如何创建?创建一个汽车类,分别创建客车,货车,皮卡类去继承汽车类创建两个接口载客接口与载货接口由三个子类继承。5.阅读下面程序,分析代码是否能编译通过,如果不能,说明原因,并进行改正。如果能,列出运行结果
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: Methods in an interface are modified by public by default, so when a subclass implements an abstract method, it should be decorated with public
And you need to implement all the abstract methods in the interface
6. Other content that needs to be summarized.

(ii) Experimental summary
This experiment includes experiment four and experiment 52 times content:
The problems encountered in the process of completing the experimental content, the solution and the design thinking and thinking of the procedure are summarized.
1 Banks
Create a bank class, define related methods and objects, and implement menu functions in the while loop and switch statement in the main function
2 Employees
Building employee classes, inherited by manager class and employee class
3 Graphics Calculation
Create an abstract class of planar graphics, and then create an abstract class of stereoscopic graphics, with different graphs inheriting different abstract classes and overriding related methods. The graph is randomly generated in the test set class and random numbers are used to generate the relevant data of the graph.

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

Java Technology's third assignment-object-oriented-inheritance, abstract class, interface

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.