1. Summary of this chapter

1.1 Try to use mind map to summarize knowledge about inheritance .



http://naotu.baidu.com/file/4de6f42e4f4f6cce0531dd9997b04e60?token=6c812969b60a8cbe

1.2 Summarize other lessons in the usual way .

Object-toString Method : commonly toString String used to return the value of an object ;toString The method is widely used : As long as the object and a string pass through the operator “+” Connect ,Java The compiler will call the object automatically toString Method .

Object-equals Method : Used to determine whether two objects have the same reference .

2. Written work

Q1: The application of annotation

Use class comments and method comments to annotate the previously written classes and methods , And in Eclipse View in .( Screenshot )

Class :



Method comments :

Q2: Object oriented design ( Big homework 1, It's very important )

2.1 Shopping in the online mall or learning in the class blog , Describe it as a story .( Not less than 50 word , Reference resources QQ In a group PPT Example of )

First, open Taobao , And log in to the account :



Enter the home page of Taobao , Enter the item I want to buy in the search box , for example :miumiu Yang Mi's small square bag



Found only one ( How few !) Then click on the product :



Choose the color you like and the quantity you need , Add to cart :



Go to shopping cart for settlement :



Choose what I want to buy in the shopping cart miumiu Small package and settlement :

2.2 Through this story, we can find out who is using the system , The classes and their attribute methods contained in the system , The relationship between classes . Try to find these classes and properties , And use mind maps to describe classes 、 attribute 、 Methods and the relationship between classes .



2.3 Try to use Java The code implements the process described in the story ( It doesn't have to be perfect ), In the future, we should gradually improve on this basis 、 Expand into a complete object-oriented system .( Optional : Bonus points )

Reference material :

UML Class diagram

Object oriented case - The name and address of the borrower .zip

Q3:ManagerTest.zip The code analysis

analysis ManagerTest.zip The code in , Answer a few questions :

3.1 In this case, where is the use of inheritance to achieve code reuse ? Which method should be used in the answer 、 Which attribute .

class Employee
{
public Employee(String n, double s, int year, int month, int day)
{
name = n;
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
hireDay = calendar.getTime();
}
public double getSalary()
{
return salary;
}
} class Manager extends Employee
{
public Manager(String n, double s, int year, int month, int day)
{
super(n, s, year, month, day);
bonus = 0;
} public double getSalary()
{
double baseSalary = super.getSalary();
return baseSalary + bonus;
}
}

among Employee For the parent class ,Manager It's a subclass (Manager extends Employee);

super(n, s, year, month, day);super keyword - Call the constructor of the parent class Employee;

super.getSalary();super keyword - Call the superclass method getSalary().

3.2 Employee Classes and subclasses Manager There are getSalary Method , How to distinguish the two methods ?

 In this case, the subclass Manager Redefined the parent class Employee Existing getSalary Method , So if you call the parent class, the method needs to be written as super.getSalary();
In this case ,JVM Which method to call depends on the type of the actual object calling the method .

3.3 Document No 26 That's ok e.getSalary(), Call Manager Class getSalary The method is still Employee Class getSalary Method .

Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
boss.setBonus(5000);
Employee[] staff = new Employee[3];
staff[0] = boss;
staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
staff[2] = new Employee("Tommy Tester", 40000, 1990, 3, 15);

According to the code, it can be concluded that :boss yes Manager class , and staff[0] = boss; here e.getSalary() call Manager Class getSalary Method ;

staff The array is Employee class , among staff[1] = new Employee,staff[2] = new Employee, here e.getSalary() call Employee Class getSalary Method .

3.4 Manager Class constructor uses super Calling the constructor of the parent class implements code reuse , What do you think is the advantage of this ? Why not copy and paste the relevant code from the parent constructor into Manager In the constructor of , Doesn't that look more intuitive ?

 The benefits of code reuse : No need to rewrite the same code , Save time .
because JVM Before executing the subclass's constructor , The constructor in the parent class is automatically called first , Its purpose is to help members inherited from the parent class perform initialization operations ,
That is to say, when creating a subclass, the parent class constructor will be called anyway , So there's no need to copy and paste the relevant code from the parent constructor into Manager In the constructor of .

Q4:Object class

4.1 Write a Fruit Classes and properties String name, If not extends From any class . Use System.out.println(new Fruit()); Is to call Fruit What's the best way ? Where does the code for this method come from ? Try to analyze what these codes do ?

 Output results :Fruit@207c15c0
When you directly output an object, you call toString Method , Is the class name and hash code of the output object ;
The code for this method is from Object From China

4.2 If Fruit Class added toString() Method , So use System.out.println(new Fruit()); Called the new toString Method . So in its parent class toString Method code is gone ? If you want to reuse the parent class at the same time toString Method , How to operate ?( Use code to demonstrate )

public class Main {
public static void main(String[] args) {
System.out.println(new Fruit());
}
} class Fruit { @Override
public String toString() {
return "Fruit [name=" + name + "]"+super.toString();
} private String name;
}

Added toString() Method, the output result is :Fruit [name=null];

In its parent class toString The code for the method still exists ;

If you want to reuse the parent class at the same time toString Method , It's simple , Direct use super keyword , Output results :Fruit [name=null]Fruit@207c15c0.

4.3 Fruit Class also inherits Object Class eqauls Method . Try to analyze its function ? Write your own equals Method to override the corresponding method of the parent class , The function is to be two Fruit object name Phase at the same time ( Ignore case ), Then the return true.( Use the code to prove that you cover eqauls The method is right )

Object Class eqauls Source code

public boolean equals(Object obj) {
return (this == obj);
}



equals() Compare references , The same thing true, The difference is false.

class Fruit {
@Override
public String toString() {
return "Fruit [name=" + name + "]"+super.toString();
} private String name; @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
} @Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Fruit other = (Fruit) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
} }

4.4 stay 4.3 Based on the use of ArrayList fruitList Store multiple fruit, Request if fruitList Existing fruit No more , If you don't have it, add it in . Please write the relevant test code . And analyze ArrayList Of contatins How does a method work ?

Q5: Code reading :PersonTest.java(abstract、 polymorphic )

5.1 Draw the inheritance relationship of the class



5.2 Read main function , Compare the result of running the code with the result of running it . Try to analyze the reasons

Real running results

Manager [bonus=12000.3, toString()=Employee [salary=90000.1, toString()=Person [name=Clark, adress=GE, phonenumber=111, email=111@mail.com, age=10, gender=mail]]]
Student [status=1, toString()=Person [name=wang, adress=110, phonenumber=15959, email=15959@163.com, age=18, gender=male]]
Employee [salary=1000.0, toString()=Person [name=zhang, adress=136, phonenumber=1360, email=1360@mail.com, age=21, gender=female]]
Programmer [allowance=50000.0, toString()=Employee [salary=100000.0, toString()=Person [name=Gates, adress=usa, phonenumber=911, email=911@com, age=59, gender=male]]]

reason :“abstract class Person”Person Class is an abstract class , So there is no instantiated object , So just look Employee,Student,Programmer,Manager These four categories ,

among Programmer,Manager Inherit Employee class , and Employee Class inheritance Person class .

5.3 Subclass uses super Constructors , What is the role ? If you put super Constructor removed , OK? ?

 Use super Constructors , The function is to call the constructor of the parent class .
no way , Because if you put super Constructor removed , The default nonparametric constructor of the parent class is called automatically , But there is no parameterless constructor in this case , So it's going to go wrong .

5.4 PersonTest.java Where does the code in reflect polymorphism ? What do you think polymorphism is good for ? What's the relationship between polymorphism and inheritance ?

 polymorphism : The same shape , Different behaviors 
· The same method name , Different implementations
· A parent type variable can refer to a subclass object
benefits : Dynamic binding (dynamic binding): The runtime can automatically choose which method to call .
Polymorphism and inheritance : Inheritance is the basis of polymorphism , Polymorphism refers to “ In many forms ” Polymorphism includes method rewriting and method overloading , It uses different instances to perform different operations
Inheritance mainly means that one class inherits the methods and properties of another class . Can achieve code reusability .

References :PersonTest.java

3. PTA Experiment summary and code cloud code submission record

3.1 This week, Commit Historical screenshot

In the project of code cloud , Choose in turn “ Statistics -Commits history - Set the time period ”, Then search and take a screenshot , As shown in the figure below

3.2 Summary of the experiment

(1) Yes toString() Methods to understand more deeply ;

(2)equals Compare references , and == Whether the objects of comparison are the same ;

(3) I have a lot of knowledge about inheritance and polymorphism , Especially subclasses and superclasses .

201521123070 《JAVA Programming 》 The first 4 Learn more articles about this week

  1. 20145213《Java Programming 》 Learning summary week nine

    20145213<Java Programming > Learning summary week nine Summary of textbook study " The May Day " The holidays go too fast , It's like a tornado . Not a little bit on guard , Just like Java Blogs are full of . In this festival of universal celebration , root ...

  2. 20145213《Java Programming 》 Second week learning summary

    20145213<Java Programming > Second week learning summary Summary of the learning content of the textbook The task given by Mr. Lou this week is to study the third chapter of the textbook -- Basic grammar . Actually, I think it's quite relaxed , Because I'm opening heavy books , A familiar breath came to my face , ...

  3. 20145213《Java Programming 》 Study summary of the first week

    20145213<Java Programming > Study summary of the first week Summary of the learning content of the textbook Looking forward to a winter vacation , Finally, I saw the mysterious teacher Lou and his Java course . Although it's not really a meeting , It's reasonable that there are countless people in the world without victory , Dan Lou ...

  4. 21045308 Liu Haoyang 《Java Programming 》 Learning summary week nine

    21045308 Liu Haoyang <Java Programming > Learning summary week nine Summary of the learning content of the textbook The first 16 Chapter Consolidate databases 16.1 JDBC introduction 16.1.1 JDBC brief introduction The database itself is a stand-alone application Write ...

  5. 20145330 Sun Wenxin 《Java Programming 》 Study summary of the first week

    20145330 Sun Wenxin <Java Programming > Study summary of the first week Summary of the learning content of the textbook It's impossible to get such a thick book without pressure at first , From the beginning, I feel strange and can't enter the state , Just a little bit of anxiety . So he ...

  6. 20145337 《Java Programming 》 Learning summary week nine

    20145337 <Java Programming > Learning summary week nine Summary of the learning content of the textbook The database itself is a stand-alone application Writing application program is to exchange instructions to database by using communication protocol , To add, delete and search data JDBC Sure ...

  7. 20145337 《Java Programming 》 Second week learning summary

    20145337 <Java Programming > Second week learning summary Summary of the learning content of the textbook Java It can be divided into basic type and class type : The basic types are integers (short.int.long). byte (byte). Floating point numbers (float ...

  8. 20145218《Java Programming 》 Study summary of the first week

    20145218 <Java Programming > Study summary of the first week Summary of the learning content of the textbook I saw it this afternoon Java Learning videos , It's very novel , I thought before Java It's hard and boring to learn , But through the study of the first chapter, I feel that if I can ...

  9. 《Java Programming 》 Learning summary week nine

    20145224 <Java Programming > Learning summary week nine Chapter 16 Consolidate databases JDBC introduction · The database itself is a stand-alone application · Writing application program is to exchange instructions to database by using communication protocol , In order to do the data analysis ...

  10. 《Java Programming 》 Second week learning summary

    20145224 Chen Haowen <Java Programming > Second week learning summary Summary of the learning content of the textbook One . type . Variables and operators 1. type Integers : It can be subdivided into short Integers ( Occupy 2 byte ),int Integers ( Occupy 4 byte ),long ...

Random recommendation

  1. Task-based asynchronous mode (TAP)

    Task .net 4.0 Brought us Task The asynchronous , We have three ways to create Task. 1,Task.Factory.StartNew, More commonly used . 2,Task.Run, yes .net 4.5 The addition of . ...

  2. About git Submit your own understanding

    Steamed stuffed bun is not just , For the code cloud git Use , My understanding is This command is used to view , Which files have been modified , And what changes have been made , This command is used to add your new files , If the file already exists , Then this step can be omitted , And then there was commit ...

  3. java Wrong questions

    1. Judgment questions : Java Procedures should generally contain main Method , Because it's all JaVa The entry of program execution ( wrong ) analysis :applet(java Applet ) no need ,application(java Applications ) need .( see java a ...

  4. Domain many-to-many

    class TakeAwardAct { String title; static hasMany=[awards:Award] static constraints = { } String toS ...

  5. Method : Inquire about MongoDB The latest data in the database (JAVA)

    Use JAVA Language query MongoDB The latest data of a certain database and a certain collection in : MongoCollection<Document> cpu = MongoClient.getDatabase(&q ...

  6. flow API-- Primitive type flow

    up to now , We've collected integers into a Stream<Integer> In the stream , But wrapping each integer into its corresponding object is obviously an inefficient approach , The same is true for other basic types , As we have said jdk The packaging class has been provided since ...

  7. hibernate in , There was a mistake &quot;node to traverse cannot be null!&quot; How to correct

    This mistake is basically due to hql It's caused by a wrong sentence , Back to find hql I'm going to find out , hql A few spaces are missing in the middle of the statement , It has been written. String hql = "from"+className+&quo ...

  8. c++ And —— lambda expression ( There is an unsolved problem waiting to be solved by the boss )——( stay stack overflow Found the answer )

    The predicate : A predicate is a callable expression , The return result is a value that can be used as a condition . There are two types of predicates used by standard library algorithms : Unary predicate , It means it only takes a single parameter : two-place predicate , It means it has two parameters . The algorithm that accepts predicate parameters calls... To elements in the input sequence ...

  9. jquery Find the drop-down box that has been selected /select

    $("select[name='select_name']").find('option:selected').text(); $('#select_name option:sel ...

  10. Django Small exercise

    1. Get all of the Url from django.urls.resolvers import RegexURLPattern # Defined function def get_all_url(patterns, prev, ...