201621123030 Java Programming 4th Week of study summary

Source: Internet
Author: User
Tags set time

1. Summary of this week's study1.1 Write down the key points of knowledge that you think are important in your study this week.

Inheritance, polymorphic, super keyword, final keyword, super keyword, object,instanceof, reload

1.2 Try to organize these keywords using mind mapping. Note: Mind mapping generally does not need to appear too many words.

1.3 Optional: Summarize other lessons using the usual methods. 2. Written work 1. Object-oriented design (large job 1-the job will be the basis for other jobs later, be sure to complete)1.1 Storytelling: Use more than 50 words to tell a story about your shopping in the online mall or the class blog. Using the Markdown Bold MarkThe key nounLabel it, use the statement block tag to put the key inside verbLabel it. Examples of storytelling: see Resources in UML class diagrams how to draw a class diagram

New School plan to buy a Java textbook online, and a few pens

Search for Java textbooks in the search box and add the most sold to the shopping cart

Then search the pen, add the lowest price to the shopping cart, and finally open the shopping cart settlement

1.2 Find out what is included in the system classAnd properties, methods, relationships between classes and classesAnd DrawingCorresponding class DiagramNote:It is not necessary to embody the inheritance relationship. As long as you can complete the system description. Do not think too much at the beginning of the design, do not appear too many classes, only the necessary classes, complete the minimum function. It is recommended to do shopping cart module only for mall shopping system.

1.3 Use Java code to implement this system (not very well). Paste run with key code. In the future, we will gradually improve and expand into a complete object-oriented system on this basis. Cooperation completed: 2-3 people a group.

Optional: team collaboration can use GIT and the code cloud. Create a new project in the code cloud. All players should have a record of submission under this item in the code cloud. Your submission record.
The main shopping cart code:

Public class User

{

Private String userName;

private String password;

private Goods Goods;

Private String address;

}

2. Managertest.zip Code Analysis (inheritance, polymorphism)

Analyze the code in Managertest.zip and answer a few questions:

2.1 Describe the inheritance relationship shown in the document. What are common methods and which are the properties and methods that are unique to subclasses?

Employee is the parent class

Public methods are:

public String getName(){}public double getSalary(){}

private double bonus就属于子类的属性

2.2 File Line 26th e.getSalary(), is the Getsalary method called the manager class or the employee class?

The staff is created as the employee class, so e.getsalary () calls the getsalary ()of the parent employee class, If the Manager object is referenced by E,E.getsalary () invokes the getsalary method in the Manger class.

What is the benefit of using the constructor of the 2.3 manager class to implement code reuse with super calling the constructor of the parent class? Why isn't it more intuitive to copy and paste the relevant code from the parent constructor into the manager's constructor?

Can reduce the number of code, avoid repeatedly knocking the same code, code reuse can greatly improve efficiency

Why not copy and paste the relevant code from the parent constructor into the Manager 's constructor?

Copy-and-paste works the same, but if we need to modify the Father function, we need to modify every place where you use the code of the parent class, there may be omissions, slow progress, lower efficiency

3. ToString and equals in the object class3.1 Write the Fruit class, property string name. If you overwrite its ToString () method, the code for the ToString method in its parent class is gone? Write the ToString () method of fruit, in which the string to invoke the ToString method of the parent class is stitched together with its own property name, how is it written? (use code to show)
 public string toString () {

return super. ToString () + "\ t" + "fruit{" + "name =" + name + '} ';

} /span>
3.2 Writes the Equals method for the fruit class to override the corresponding method of the parent class, which returns True when two fruit objects are the same name (ignoring case). (use code to prove that the Equals method you covered is correct)

Open the source code for the eqauls method of the Object class:

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

If you call the equals method of the object class, you need to start with a new two object, so the answer is false, although the object name is the same, but the object is different. This inevitably reminds us that there is also a equals method in the String class, and when we use this method, the answer we get is true.

        Fruit[] fruits = new Fruit[2];        fruits[0] = new Fruit("apple") ; fruits[1] = new Fruit("apple"); System.out.println(fruits[0].equals(fruits[1]));

The run result is false;

        String str1 = new String("apple");        String str2 = "apple"; System.out.println(str1.equals(str2));

The run result is true.
Why is the same equals method, and the answer is the opposite? The nature of the equals method of the object class is actually the same as "= =", comparing whether two object references point to the same object (that is, two objects are the same object); According to code,str1 and str2 is pointing to the same string pool in memory as the Apple string, then the answer is naturally true .
Next, rewrite the equals method:

public Boolean equals (ObjectObject) {if (this = =ObjectReturnTrueif (! (object instanceof Fruit) return FALSE; fruit Fruit = (fruit) object; if (this.name = = null) {if (fruit.name! = null) return FALSE; } else if (! This.name.equalsIgnoreCase (fruit.name)) return false; return true;}         

This rewrite code was written by myself, and later I found out that the compiler would generate the equals method itself, and that it could be accompanied by rewriting the hash function:

    @OverridePublicIntHashcode() {Finalint prime =31;int result =1; result = Prime * result + ((name = =NULL)?0:name.hashcode ());return result; }@OverridePublicBooleanequals(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 Span class= "Hljs-keyword" >FALSE; return true;}        

If the object of this input class is not the Fruit class We want, it returns falsedirectly, if name is null, and the name entered is not NULL, also returns false , in order to ignore the case, I used the equalsignorecase method in the String class, as long as the Object class was judged wrong, I return true.
My two objects are:
  
The result of this operation is naturally true .

3.3 After completing 3.2, use ArrayList

Tip: Use ArrayList's contains method directly to determine whether an object exists.


Operation Result:

4. Experiment Summary:4.1 PTA Programming Questions (Shape-inheritance). And answer: What are the benefits of using polymorphism for programming in this subject?

Polymorphic changes to code authoring are much simpler and more customizable, reducing the time to write

4.2 PTA Programming problem (overlay). And answer: What do you need to pay attention to when writing the Eqauls method?

When writing to equals remember that you cannot forget the null condition

4.3 Procedural blanks and Function questions (1-3)

6-1 to invoke the ToString of the parent class with super

6-2 This problem is based on the question request step by step, and finally the reverse output array if NULL does not output

6-3 This problem with the method of equals, automatically generated after you remember to modify the slary part

3. Code Cloud and PTA 3.1. Code cloud codes Submission record
    • In the Code cloud Project, select statistics-commits history-set time period, and then search for and

3.2 PTA Problem set complete situation diagram

Completed this week (Shape-inherit, overwrite)
Two graphs are required (1. Ranking. 2.PTA Submission list)

3.3 count The amount of code completed this week

The weekly code statistics need to be fused into a single table.

Week Time Total code Amount New Code Volume total number of files number of new files
2 330 330 5 5
3 625 322 11 6
4 1047 422 16 5

201621123030 Java Programming 4th Week of study summary

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.