Note: Lecture 3 object oriented features in the Java language

[thinking before class] 1. What are objects? What are classes? What are packages? What are interfaces? What are internal classes? 2. What are the three features of object-oriented programming? What are their respective features? 3. Do you know the unique features of Java language in object-oriented programming?

Difficulties: 1. Understand method overloading and method rewriting, and don't confuse the use of the two. 2. 2. Use of class variables and class methods. 3. Use of interface. 3.1 fundamentals of object-oriented technology

3.1.1 basic concept of object-oriented object-oriented basic idea object-oriented is a new programming method, or a new programming specification (paradigm). Its basic idea is to use the basic concepts of object, class, inheritance, encapsulation, message and so on. Things that exist objectively from the real world (i.e. object) to construct software system, and use human natural thinking mode in system construction as much as possible. A software is developed to solve some problems. The business scope involved in these problems is called the problem domain of the software. Its application field is not only software, but also computer architecture and artificial intelligence.

1. Basic concept of object object is an entity used to describe objective things in the system. It is a basic unit of the system. An object consists of a set of properties and a set of services that operate on these properties.

An active object is an encapsulation of a set of attributes and a set of services, At least one of these services can be actively executed without receiving messages (called active service). 2. The basic concept of class. Class is a collection of objects with the same attributes and services. It provides a unified abstract description for all objects belonging to this class. It includes two main parts: attributes and services. In object-oriented programming language, class is an independent program unit. It should have a class name and include attribute description and The service description has two main parts.

3. Message

A message is a service request sent to an object. It should contain the following information: object ID, service ID, input information and answer information. Services are often referred to as methods or functions.

3.1.2 basic characteristics of object-oriented

1. Encapsulation encapsulation is to combine the attributes and services of the object into an independent same unit, and hide the internal details of the object as much as possible, including two meanings: ◇ combine all the attributes and services of the object, Form an indivisible independent unit (i.e. object). ◇ information concealment means concealing the internal details of the object as much as possible, forming a boundary [or forming a barrier] to the outside, and retaining only a limited external interface to make it contact with the outside. The reflection of the principle of encapsulation in the software is that the parts other than the object cannot access the internal data of the object at will (attribute), so as to effectively avoid the "cross infection" of external errors, localize software errors, and greatly reduce the difficulty of error checking and troubleshooting.

2. The object of an inherited special class has all the properties and services of its general class, which is called the inheritance of a special class from a general class.

A class can be a special class of multiple general classes. It inherits properties and services from multiple general classes, which is called multiple inheritance.

In the Java language, we usually call a general class a superclass and a special class a subclass.

1. Class declaration: [public] [abstract|final] class classname [extends superclassname] [implements interfacenamelist] {...} where the modifiers public, abstract and final describe the properties of the class, classname is the class name, superclassname is the name of the parent class of the class, and interfacenamelist is the list of interfaces implemented by the class. 2. Class body class body is defined as follows: class classname {[public | protected | private] [static] [final] [transient] [volatile] type variablename; / / member variable [public | protected | private] [static] [final | Abstract] [native] [synchronized] ReturnType methodname ([paramlist]) [throws exceptionlist] {statements} / / member method} 3. The member variable is declared as follows: [public | protected | private] [static] [final] [transient] [volatile] type variablename; / / member variable, where, Static: static variable (class variable); relative to the instance variable, final: constant, transient: temporary variable, used for object archiving and object serialization. See the serialization section of objects. Volatile: contribution variable, used for sharing of concurrent threads. 4. The implementation of member methods includes two parts: method declaration and method body [public | protected | private] [static] [final | Abstract] [native] [synchronized] ReturnType methodname ([paramlist]) [throws exceptionlist] / / method declaration {statements} / / the meaning of the qualifier in the method declaration of the method body: static: class method. You can call the abstract: abstract method directly through the class name, No method body final: methods cannot be rewritten native: integrate code from other languages synchronized: control access to multiple concurrent threads ◇ method declaration method declaration includes method name, return type and external parameters. The type of parameter can be simple data type, It can also be a composite data type (also known as reference data type). For simple data types, Java implements value passing. Methods receive the values of parameters, but cannot change the values of these parameters. If you want to change the values of parameters, use reference data type, because the reference data type passes the address of data in memory to the method, and the operation on data in the method can change the value of data 。 Example 3-1 illustrates the difference between simple data types and reference data. [example 3-1] import Java. Io. *; public class passtest {float ptvalue; public static void main (string args []) {int Val; passtest Pt = new passtest(); Val = 11; system.out.println ("original int value is:" + VAL); pt.changeint (VAL) ; / / value parameter system out. println("Int Value after Change is:" +val); /* The modification of the value parameter value does not affect the value of the value parameter * / Pt ptValue=101f;   System. out. println("Original ptValue is:"+pt.ptValue);   pt. changeObjValue(pt); // Parameter of reference type system out. println("ptValue after Change is:"+pt.ptValue);/* Modification of reference parameter values, Changed the value of the reference parameter * /} public void changeint (int value) {value = 55; / / modified the value parameter inside the method} public void changeobjvalue (passtest ref) {ref.ptvalue = 99F; / / modified the reference parameter inside the method}}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>