[Java] Java Record

Source: Internet
Author: User

2-1 encapsulation Concept in Java: To hide some of the information of a class inside a class, not to allow direct access by external programs, but to implement operations and access to hidden information through the method classes provided by the class. Benefits: 1. Data 2 can only be accessed through the prescribed method. Hides the instance details of the class for easy modification and implementation of encapsulation steps: 1. The visibility of the modified property is set to Private2. The Create Getter/setter method is used to read and write 3 properties. Adding attribute control statements to the Getter/setter method to determine the legitimacy of a property 2-2 use the Package management class 2-3 access modifiers in Java--can be decorated with access to the properties and methods access modifiers into similar bun class other private √ Default √√protected√√√public√√√√2-4 The This keyword in Java 1. The This keyword represents the current object of this. Property Action property of the current object the This. Method operation Method 2 of the current object. The This keyword is often used when encapsulating the properties of an object 2-5 what is the inner class in Java Q: What is an inner class? A: The Inner class (Inner Class) is the class defined in another class. Corresponding to this, the class containing the inner class is called the outer class. Q: Why do you define a class in another class? Clear refreshing cool independent of a kind of how good ah!! A: The main functions of the inner class are as follows: 1. The inner class provides a better encapsulation that hides the inner class within the outer class and does not allow other classes in the same package to access the Class 2. The methods of the inner class can access all the data of the external class directly, including the private data 3. The functionality implemented by inner classes can also be implemented using an external class, but sometimes it is easier to use inner classes to ask: How many internal classes are there? A: Internal classes can be divided into the following: member inner class static inner class method inner class anonymous inner class 2-6 Java member inner class COM.IMOOC2.DEMO05 member inner class use method: 1, Inner class is defined within the Outer class, equivalent to a Outer class The Inner class can use any access control, such as the test () method defined in the public, protected, private, etc. 2, Inner class to directly access the data in the Outer class without being affected by the access control, such as direct access to O After the private attribute in the Uter class is A3 and the member inner class is defined, the Outer class object must be used to create the inner class object, rather than the inner class object name = Outer class object. New inner Class (); 4, after compiling the above program, we will find that two. class files where the second is the. class file for the outer class, the first is the. class file for the inner class, which is always the. class file for the inner class of the member. The external class name $ internal class name. Class In addition, friendship tips OH: 1, An outer class is an object that cannot be used directly by the members and methods of an inner class to create an inner class, and then accesses its member variables and methods through an object of the inner class. 2. If the external class and the inner class have the same member variable or method, the inner class accesses its own member variable or method by default, and if you want to access the member variable of the external class, you can use the This keyword. 2-7 static inner Classes in Java com.imooc2.Demo06 static inner classes are internal classes of static adornments, which are characterized by: 1, static inner classes cannot directly access non-static members of external classes, but can be accessed through the new external class (). member Access 2, If the static member of the outer class is the same as the member name of the inner class, the static member of the external class can be accessed through the class name. Static member, if the static member of the outer class is not the same as the member name of the inner class, the static member 3 of the outer class can be called directly through the member name, You do not need an object of an external class, you can directly create an inner class object name = New inner Class (); 2-8 method Inner class in Java The inner class of the Com.imooc2.Demo07 method is the inner class defined in the method of the outer class, and the inner class of the method is visible only within the method, that is, it can be used only inside the method. It is important to note that because the method inner class cannot be used outside of the methods of the outer class, the method inner class cannot use the access control and the static modifier. 3-1 inheritance keyword in Java: The initialization order of extends inheritance 1. Initializes the parent class, and then initializes subclass 2. Initializes the properties in the object, and then executes the initialization of the constructor in the parent class, the parent class property initialization, the parent class constructor method, subclass object, subclass property initialization, subclass constructor, child class construction method, using the final keyword for final use in Java To make an identity with "final" meaning, final can decorate the class, method, property, and variable final decorated class, the class does not allow to be inherited by the final decoration method, the method does not allow overridden (overridden) The final decoration property, the class's properties are not implicitly initialized , or assign a value in a construction method (but only one) to the final modifier variable, the value of the variable can only be assigned once, that is, a constant 3-6 the Super keyword in Java is used inside the object and can represent the parent class object. 1. Access the parent class's property super.age2. Access the parent class method Super.eat () 3. Super uses the constructor of the subclass to call the constructor of its parent class, when we create the subclass object, we actually do it automatically, create the parent class object, and initialize the parent class construction method, and we have implicitly used the Super keyword when we need to use the Super keyword explicitly. You must put the super () parent class construction method into the first row of the subclass construction method. Constructor call must is the first statement in a constructor if the constructor method of the parent class is not explicitly called in the subclass constructor, and the parent class does not have an argument constructor, then the compilation error occurs. 3-7 the object class in Java is the parent class of all subclasses, and if a class does not explicitly identify inheriting another class using the extends keyword, the class inherits the object class by default. Method in the object class, suitable for all subclasses. Common methods in the object class: 1. ToString (): Default returns the hash code (object address string) of the object when the ToString () method is defined in the object class; the ToString () method can be overridden to represent the object's property @overridepublic void ToString () {return "dog[age=" +age+ "]";} 2. Equals (): Compares the reference of the object to the same block of memory address; For example: Dog dog = new Dog (); The dog created at this time is the one that created the dog reference to an address in memory, and by calling the address in memory of the dog object, Indirect use of objects however, in general, when you compare two objects, you are comparing the values of two objects, so you need to override this equals method. @overridepublic boolean equals (Object obj) {if (this==obj) return true;if (Obj==null) return false;if (GetClass ()! = Obj.getclass ()) return False;dog other= (Dog) obj;if (age!=other.age) return False;return true; 4-1 polymorphic 1 in Java. References to referenced polymorphic parent classes can point to objects of this class: Animal obj1 =new Animal (); References to parent classes can point to objects of subclasses: Animal obj2 =new Dog (); 2. Method of polymorphic creation of this classObject, when the method is called to create a subclass object for a method of this class, the calling method is a subclass of the overridden method or the inherited method 4-2 the reference type in Java to convert 1. Up-type conversions (implicit/automatic type conversions), which are small types to large types of conversions, do not present a risk, also called automatic type conversion 2. The downward type conversion (coercion type conversion), which is a large type to a small type of conversion exists at risk, can occur when the small type does not fit into the large type, data overflow 3. The instanceof operator to resolve the type of the referenced object and avoid the security of the type conversion. Determines whether a reference is a type or a subtype of a type, returns a Boolean value 4-3 the abstract class abstract class in Java before it is used as an abstract class scenario: 1. In some cases, a parent class simply knows what method a subclass contains, but does not know exactly how these subclasses implement these methods. That is to say, abstract class is the method that the subclass must have, but does not care about how to implement the subclass 2. Abstract an abstract class from multiple classes with the same characteristics, with this abstract class as a subclass template, so as to avoid the arbitrariness of sub-class design. Role: Restriction subclasses must implement some methods, but do not care about the details of implementation, but what are the characteristics of constrained subclasses. Usage rules: 1. Abstract class 2 is defined. Abstract defines an abstraction method that is only declared and does not need to implement 3. The class that contains the abstract method is abstract class 4. Abstract classes can contain normal methods, or they can have no abstract method 5. Abstract classes cannot be created directly, you can define reference variables 4-4 interface interfaces in Java can be understood as a special class, composed of global constants and public abstract methods. 1. The interface concept class is a specific implementation body, and several ports define the compliance specifications required for a batch of classes, the interface does not care about the internal data of these classes, and does not care about the implementation details of the methods in these classes, it only stipulates that certain methods must be provided in these classes. 2. The interface definition differs from the class definition by defining that the interface no longer uses the class keyword, but instead uses the interface keyword. 3. Basic syntax for definition: [modifier] Interface Interface Name [extends parent interface 1, parent Interface 2 ...] {0 to more constant definitions ... 0 to multiple abstract method definitions ...} Interfaces are used to be inherited, implemented, modifiers are generally used public Note: You cannot use private and protect to decorate the interface constants: The attributes in the interface are constants, even if the public static final modifier is not added when defined, the system is added automatically. Method: The method in the interface is only an abstract method, always used, even if the definition does not add the public abstract modifier, the system will automatically add.4. Using an interface a class can implement one or more interfaces, implementing an interface using the Implements keyword. A class in Java can only inherit one parent class, which is not flexible enough to be supplemented by implementing multiple interfaces. The syntax for inheriting a parent class implementation interface is: [modifier] class name extends parent class implements interface 1, interface 2 ... {The class body part///If you inherit an abstract class, you need to implement an abstract method of inheritance, to implement an abstract method in an interface. If you want to inherit the parent class, the inheriting parent must precede the implementation of the interface, meaning that the keyword extends must precede implements. * For example, there is a parent abstract class called "Phone Telphone", containing two subclasses, namely "cell phone cellphone" and "smart phone smartphone", cellphone can call and send text messages via the keyboard, Smartphone can call and send text messages by voice, and at this time smartphone can also be used to play games, for smartphone with the function of playing games, smartphone and PSP have the function of playing games, can not let the PSP inherit abstract class Telphone , because the PSP and Telphone are not the same type, but they have the same characteristics (play the game), then you can consider using an interface to describe this feature, the function of playing the game is defined as an interface * through the interface can describe the two different types of objects common characteristics, and use them through the interface. 5. The use of interface interfaces is often used in conjunction with anonymous internal classes during use. Anonymous inner class: is an inner class without a name, and is used to focus on implementation rather than on the name of the implementation class. Interface i = new Interface () {public void method () {System.out.println ("How the anonymous inner class implements the interface");}}; 4-7 Introduction to UML 1. The UML Concept Unified Modeling Language (UML), also known as the Unified Modeling Language, or the standard modeling language is a graphical language that supports modelling and software system development, providing modeling and visualization support for all phases of software development. 2. UML Diagram UML 2.2 defines a total of 14 illustrations (diagrams) 3. Common UML diagram A. Use case diagrams (the use case Diagram) can be visualized to express how the system satisfies the collected business rules, as well as the specific user needs and other information. Restaurant (Simplified)-->eat foodfood critic-->pay for food-->drink Winecook food<---Chef in a restaurant, ordinary users can eat, pay for what they eat, drink, and cook to eat. This allows you to combine the roles in the system with the system functions. B. Sequence diagram (the Sequence Diagram) sequence diagram mainly describes the relationship between roles and objects in computer memory, and in what order the relationship is executed, we pay more attention to this sequence. Used to display these interactions between objects in a sequence of interactions that occur. ----------------------------------------------------------User Counter Window Billing window withdraw Money account Backstage | | | | | | show identity-"| request to withdraw money |" Confirm and withdraw Money | | | | | | | -Distribution Currency |-Authorized Legal-| | | | | | |----------------------------------------------------------For example, the user to the bank to withdraw money, then this process of taking money, Is the relationship between the user and the system, first of all identify the identity, and then request to withdraw money, finally confirm the money. C. class diagram (the Class Diagram) UML class diagrams, business logic, and all supporting structures are used together to define the entire code structure. The main description is the business logic, and there are some structural information, usually used to describe the relationship between classes and classes, which properties, What are the methods.-----------------------------| animal| class name-----------------------------|-name:java.lang.string| property |-age:int |-----------------------------|+ Eat () : Void| method-----------------------------∧|-----------------------------| Dog| class name-----------------------------| | -----------------------------|+< >eat (): The Void| method-----------------------------the Dog Inheritance Animal property and method, marking the Eat () method that the dog rewrote Animal; *uml can be from different links, To understand the functionality of the system so that it is more precise and more thorough in writing the system. D. Modeling tools Visio, Rational Rose, powerdesign, Enterprise Architect modeling Tools Applications Http://www.uml.org.cn/umltools 6-1 comprehensive exercises based on the knowledge learned, Write a console version of the "Car Rental system" function: 1. Show all available Cars 2. Choose a car, hire 3. Display car rental list, including: Total amount, total cargo capacity and its model, total manned quantity and its model item analysis: 1. Data Model analysis A. By analyzing and abstracting the main features of the things and objects that show the world, the data structure and the corresponding constraints are provided for the implementation of the information system. B. Data structure Composition: operation (method), attribute 2. Business model analysis A. Before you design your application, you should identify which tasks the application must perform to analyze the business requirements is one of the most important steps in application development to identify the business requirements in order to create a solution that meets both the retailer and consumer needs. B. Only the consumer business needs to be considered in the car rental system. Consumer business needs: * Select vehicle * Car Rental days * Statistics Amount * passenger, Volume 3. Display and process analysis a. Display: User can see the information in the Tip interface B. Process: Displays the process of information execution, steps. C. In the car rental system, to display the prompt information and output information in the command line mode, to consider its style, the user input different data, how to prompt the information, how to process and show the results, this part of the knowledge includes the content of the display and the process. For example: Please select vehicle, please enter serial number, output total amount ...

[Java] Java record

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.