1. This week's learning summary

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

1.2 Summarize other lessons in the usual way .

  • 1.1 Knowledge about inheritance :

  • 1.2 About polymorphism

  1. polymorphism : The same shape , Different behaviors . This is reflected in the same method name , Different implementations , And superclass type variables can refer to subclass objects .
  2. Parents and children have the same Methods , Run means that the compiler can automatically choose which method to call .
  3. Polymorphism can solve the problem of too much redundant code in the same code , You can dynamically change a small amount of code to meet your needs .

2. Written work

1. The application of annotation

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

Eclipse Note templates can be viewed in the following ways :

Window->Preferences->Java->Code Style->Code Templates->Comments

Here are some comments :


Comment on attribute

get Method comments

set Method comments

Comments on construction methods

Comments for override methods

2. 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 )

The season is about to change , I'm going to buy some clothes on Taobao , So I opened Taobao taobao links, Log in to my Taobao

After successful login , You can see the one on the left Theme Market , Choose women's wear

Entered the women's clothing market , Now you can browse all kinds of women's wear !

Choose a dress , Then you can see all kinds of beautiful dresses , Then I picked one I like , Add to cart

There are a lot of things I add in the shopping cart , It shows the information of the item, mainly the trade name , The unit price , Number , There are other operations such as deletion

And finally Selected products , total , And then there's a Settlement . But if you can't afford it, you won't settle it , Let the shopping cart wait for the baby .

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 between classes ### The relationship between .

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 )

The system is, of course “ I ”, That is, consumers are using , The classes that may be included in the system are startup classes , For logging in to the system ,main Method is in it , Then there is the topic class , Commodity type , Shopping carts and so on .

3. 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 .

The first 77 Line use inheritance Employee Class with parameters (n、s、year、month、day) Constructors ,

The first 83 Line use inheritance Employee Class getSalary() Method ,salary;

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

Subclass Manager Medium getSalary The way is right Employee Class getSalary Method overloading , The difference lies in what objects are created , For example, if you create Employee Class object , It's called Employee Class getSalary Method .

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

Yes 25 The line code knows the variable e yes Employee Class pointing staff Array objects , At run time according to staff Different objects , And call the corresponding 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 ?

Can be less code , No need to add extra data , Save time , Increase the practicability of the program ;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 . therefore , If you copy and paste the relevant code in the parent class constructor into the Manager There will be an error in the constructor of , Or let me Manager Class does not inherit Employee class ,Manager Redefining the constructor of , But it's obviously more time-consuming , Therefore, it is not recommended to change it in this way , Inheritance should be used to achieve code reuse .

4. 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 ?

The output is Fruit@75a1cd57

We put the first 6 OK, replace it with System.out.println(new Fruit().toString()); It is found that the output is unchanged .

stay jdk6.ZN_zn.chm It can be found in the survey println On the realization of object :

It can be seen that the final return is Fruit.toString() Value .

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 the same ### You want to reuse the toString Method , How to operate ?( Use code to demonstrate )

Any action on a subclass does not change the parent , Of the parent class toString The method still exists , And subclasses have one toString Method , Is a subclass specific method . The demonstration is as follows :

The output is Fruit [name=null] and Food [name=null], Because there is no assignment , therefore name All for null.

If you want to reuse the toString Method , In the first 15 OK, change it like this :

The output is

Fruit [name=null]

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 )

There's an easy way to make people Eclipse Automatic generation equals Method , Press and hold at the same time shift+alt+s key , choice Generate hashCode() and equals()... There are ready-made ones equals The method .

On request name Ignore case , So in the first 29 All right name.equals() Change to name.equalsIgnoreCase(), The other is constant .

Output results :

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 ?

5. 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

Output results , And the operation is effective Debug

people 5000.0

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

Reuse the code of the constructor of the parent class , If you remove , An error will be displayed Implicit super constructor Person3() is undefined. Must explicitly invoke another constructor, Of course, you can write the code of the constructor of the parent class again , But it's a lot of code , So it can't be removed super function .

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 ?

  • Every class has a public String toString() function ;
  • Parent type variables refer to child objects :
Employee3 [] people=new Employee3[5];
Person3 p=new Student3("lisi","putian","88","12","23","male","high");
people[0]=new Employee3("lisi","putian","88","12","23","male","school",5000.0,"365 God ");
people[1]=new Employee3("wangwu","putian","89","12","23","fmale","school",500.0,"36 God ");
people[2]=new Employee3("wen","putian","88","12","23","male","school",5000.0,"365 God ");
people[3]=new Employee3("yang","putian","89","12","23","fmale","school",50000.0,"36 God ");
  • There are two forms of polymorphism: overloading and covering . Overloading is represented by having two or more functions in a class , It's the same name and they have different parameters . Coverage can only occur in subclasses, that is to say, only when there is inheritance can coverage occur . Inherit a class , So you have all the methods of the parent class , If some methods and functions need to be changed , Then re implement that function in the subclass . So when you call this method again , Is the process of executing subclasses , The functions in the parent class are covered .

  • Inheritance is that a subclass inherits all the properties and methods of a parent class , Polymorphism is a parent class reference and can be a child class object , We take the parent type as an argument in the method , Various ways to invoke the parent class in method implementation , When subclasses are passed as parameters to this method ,java The virtual opportunity is based on the type of object actually created , Call the corresponding method in the subclass , That is, look at the parent class at compile time , Look at subclasses at runtime .

3. Code cloud code submission record

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

4. PTA experiment

Topic collection :jmu-Java-03- object-oriented 1- Basics - Encapsulation inheritance Medium

function (4-1, 4-2, 4-3,4-4 It's hard to choose )

Programming (5-4, 5-5, 5-6)

  • You can save a lot of code through inheritance , Realize dynamic binding
  • The subclass inherits all the properties and methods of the parent class , If subclasses need to implement functional changes , Just override the method of the parent class .

201521123007《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. (Array)121. Best Time to Buy and Sell Stock

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  2. MVC-RedirectToAction Jump to something else Area

    mvc Use Area After partition development , Different exist Area Jump between , You need to add Area The rules , as follows : using System.Web.Mvc; namespace web.Areas.FrameSet ...

  3. C# In the program : How to talk to xml Insert node in file ( data )

    towards xml Add node dynamically in file ( data ) It's a great thing , Can bring a lot of convenience to your program , For example web in , If your Flash Yes xml file , This method allows you to easily update your database in the background Flash Content ! Research together ...

  4. gulp Configuration version number Tutorial of gulp-rev-append

    brief introduction : Use gulp-rev-append Add a version number to the reference of the page , Clear page reference cache . 1. install nodejs/ Global installation gulp/ Project installation gulp/ establish package.json and gulpfile.js file ...

  5. android Stack exit

    Be situated between list The exit mode will overflow the memory , Use your own way to maintain the stack . Reference resources : http://www.2cto.com/kf/201312/265523.html http://www.cnblogs.com/ma ...

  6. thinkphp File upload

    Detailed explanation of file upload http://www.thinkphp.cn/info/194.html Upload root directory does not exist problem solution http://www.thinkphp.cn/topic/10779.html

  7. [Swift]LeetCode909. Snake ladder | Snakes and Ladders

    On an N x N board, the numbers from 1 to N*N are written boustrophedonically starting from the botto ...

  8. vue-video-player Integrate videojs-contrib-hls Realization .m3u8 File playback

    // Installation dependency npm install vue-video-player --save npm install videojs-contrib-hls --save // stay main.js Global import in ...

  9. [LeetCode] Most Profit Assigning Work Arrange the most profitable work

    We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith ...

  10. Ubuntu16.04 And Solr7.7.1 Environment building

    Solr The version of is changing all the time ( For example, I wrote about it in the blog Garden before Linux install solr, that solr by 7.6 edition , It's no longer here ). You can go to this address to download the corresponding version :http://mirror.bit.edu. ...