20,182,320 2019-2020-1 "data structures and object-oriented programming," the first four weeks learning summary

20,182,320 2019-2020-1 "data structures and object-oriented programming," the first four weeks learning summary

1. learning content summary

This week learning content is divided into the following sections:

  • 4.8.1 iterators and a for loop
  • 5.1 talk about classes and objects
  • Analysis of class 5.2
  • 5.3 package
  • 5.4 Method of analysis
  • 5.5 static class members
  • 5.6 class relationships
  • 5.7 Method Design
  • 5.8 Method overloading
  • 5.9 Test
  • 5.10 Debugging

4.8.1 iterators and a for loop

What is an iterator?

  • Iterator is a subject that we can use its methods to deal with the elements of a collection, once a deal.
  • Each object has a method iterator hasNext , which returns a Boolean value. Show me the next time is really yet to be processed, is false when no explanation.
  • There iterator a next method, you may acquire the data items in the collection.
  • Examples: Scanner object class definition iterators.

Iterators and a for loop:

  • When the object is an iterator class implements the Iterable interface , it can be used for modification of each loop processing.
  • This code, for example, the following:
for (Book myBook : bookList)
     System.out.println(mybook);

It can be more finely resolved into:

Book myBook;
while(boookList.hasNext())
{
    mybook=bookList.next();
    System.out.println(myBook);
}

Detailed: bookList is a Iterable object that holds objects Book, then loops processing each Book object with the iterator for.

Above this cycle is called for-each statement .

  • Scanner is an iterator class, but does not implement the Iterable interface, so it has hasNext and next but can not use the above-described method of this cycle.

5.1 talk about classes and objects

  • Recalling the relationship between classes and objects:

Class is a blueprint for an object.

Class represents a concept, the object is to achieve this concept.

  • Each object has a state (State) , which related to the object by a defined attribute (attribute) , the attributes defined by the variables declared in the class .
  • Each object has a behavior (behavior) , that the objects associated with the defined operation (Operation) , and the behavior is defined by a method declared in the class .
  • Design program is divided into two parts as follows:

Determining the object class and

The allocation of responsibilities

Analysis of class 5.2

  • A class can contain data declarations and method declarations two parts, they are all class members .
  • Method declaration is also divided into construction methods and conventional methods . Constructor methods named classes, defined service object provides a common method.
  • If the file contains a plurality of classes, by only one class with the reserved word public declarations.
  • Public class name must be the same file name.
  • Examples of data refers to a class declared variable. They can be any of the methods referenced class.

5.3 package

  • Packaging is a characteristic of the object. It means that the variables of an object by its own management , generally does not allow direct access to other objects and can only be accessed through a specific method such characteristics.
  • Visibility modifiers total of three: public (the public), private (private) and protected (protection) .

modified public class members can be referenced directly outside the object ; private modified class members can only be used in their definition in the class ; protected typically for inclusion in the inheritance relationship class, its class in the same package can at direct reference , between the public and private interposed encapsulation.

  • A class that provides access and modify data service value of is access method (accessor method) and setter (mutator Method,) , commonly called "variable name get + access" and "set + modify variable names."

5.4 Method of analysis

  • The method of the structure, for example:
public int computeArea(int length,int width)
{
    int area=length*width;
    return area;
}

public-- modifier

int-- return type

computeArea-- method name (identifier)

int length/int width——参数

(Optional) Throws clause, indicating that the method may throw an exception

Content in the {} - Method body

  • The return statement return header specifies the type of the return value
  • void return type does not contain a return statement or contain only "return;" statement
  • Constructor does not return type of the return value
  • After the return statement returns control immediately call position and continue
  • The method is called the head parameters declared parameter (Parameter Formal) , passed to the method is called when calling the method the value of the argument (Actual Parameter) .
  • The method is called the local data declarations, can only be used in the method; called instance data declared in the class, it can be used in any method in the class.
  • Constructor to initialize variables corresponding to each object , the name must be the same as the class name .

5.5 static class members

  • Static variables declared by the static modifier, it is shared by all instances of the class.
  • Local variables can not be static.
  • Static methods can be directly invoked by class name, you do not need to instantiate an object.

5.6 class relations

  • Dependence: a class using another class
  • Polymerization: an object by the other objects, the object has become polymerized to form a has-a relationship
  • this reference: to an object that is currently running

5.8 Method overloading

  • The method of overload refers to a plurality of different methods using the same method name , but between them according to the different number of parameters to distinguish which method is to be invoked.

5.9 Test

  • Unit Test: create a test case for each module (Method)
  • Test-driven development (TDD test):

To write test cases

Then write the source code to ensure that test cases by

Textbook learning and problem-solving process

  • Question 1: Why constants can be public?
  • Problems Solution 1: leafing through the book, that, as a constant can not be modified, so the package is set to not to alter the characteristics of the public.
  • Question 2: Why class needs package?
  • Problem 2 Solution:

CSDN by consulting various forums and Baidu know, etc., are summarized as follows:

Code rewriting reduction rate and increase reuse

Compile error rate reduction

To facilitate third-party call

Security assurance classes

Code debugging and problem solving in the process

  • Question 1: When writing PP5.3, create enumerated types Side and its variable side, after the assignment of its output does not know how it
  • Problem 1 Solution: 3.6 until you see the book program, probably understand how enumerated types output.

  • Question 2: In writing PP5.3, reference Coin class, I do not know what the meaning of this code is below
  • Problem 2 Solution: According to the superficial impression of the C language and personal understanding, I think the meaning is: the detection parentheses two values ​​are equal. If equal "Heads" is returned; if not equal "Tails" is returned.

  • Question 3: When writing PP5.6 in BoxTest, simply want to be as follows crossed codes to change an object's fill state box2, tune into the full, and found that such a change can not seem ......
  • Question 3 solution: to replace the code into a tick, you can complete this function.

  • Question 4: experienced above changes, I found the above problem-solving method does not change the value of 3 full at the time of running the program, even if the following changes to the main function can not be achieved.
  • Question 4 Solution: It may be because of the scope, I make changes directly in the test method, you will be able to successfully modify the value box2 success.

  • Question 5: At the time of the preparation of operating procedures PP5.13 found that the resulting cards are the same suit.
  • Question 5 Solution: I found when generating random numbers using casts of the code section, not to enclose the whole, results in the generation of random numbers is only one value, you can add parentheses.

Code hosting

Last week exam wrong question summary

Wrong question 1


Reason: \ T is a tab, \ n is a newline, \ r is a carriage return. The output is:

1 big bad wolf  8 the 3 little pigs
4 dinner2night

2 wrong questions


The reason: Reserved words are systems with special characters predefined, println is a method of System.out.object.

3 wrong questions


Reason: Remainder calculation operator% can be used for all types of characters

Pair peer review and

Comments had students blog and code

  • Pair this week learning
    • 20182307
    • Pair Photo
    • Pair learning content
      • Java cryptographic algorithms
      • Writing class
  • Last week blog peer assessment case

Other (perception, thinking, etc., optional)

Java class code would be understandable after careful analysis of the structure, which provides the interface - the channels of communication between programs is a very sophisticated design.

Learning progress bar

The number of lines of code (add / accumulate) Blog amount (add / accumulate) Learning time (add / accumulate) Important growth
aims 10,000 lines 30 400 hours
the first week 208/208 2/2 9/9
the second week 258/466 2/4 15/24
The third week 693/1159 2/6 22/46

Guess you like

Origin www.cnblogs.com/leonzheng/p/11610446.html