1: Process and object-oriented differences:
Why are object-oriented analysis methods present? because the real world is too complex and changeable, process-oriented analysis methods can not meet the process-oriented? Process-oriented processes must be understood throughout the process, each step has a causal relationship, each causal relationship constitutes a step, multiple steps constitute a system, because there is
Causality each step is difficult to separate, very tight, and when any of the steps arise, the problem will affect all systems. Such as: the use of process-oriented production of computers, then he will not divide the CPU, motherboard and hard disk, it will be based on the computer's workflow once molded. Object oriented? Object-oriented pairs divide the real world into different units (objects), implement individual objects, and, if a function is accomplished, you can simply collaborate on the individual objects.
2: Object-oriented three main features:
All things are objects, reflecting the real world of specific things
Package (TV box)
Inherit (father has money, son has money)
Polymorphism (the ability of the same behavior to have many different manifestations or forms)
Classes in Java
A class is an abstract description of a common thing that describes the behavior and state of a class of objects (the abstract process, which does not actually exist in the reference data type), based on object-oriented can
A class can contain the following types of variables
Local variables:
Variables defined in a method, construction method, or block of statements are called local variables. Variable declaration and initialization are all in the method, and when the method finishes, the variable is automatically destroyed
Member variables:
Member variables are variables that are defined in the class, outside the method body. This variable is instantiated when the object is created. Member variables can be accessed by methods, construction methods, and statement blocks of specific classes within a class.
Class variables: Class variables are also declared in the class, outside the method body, but must be declared as static types.
3: Class-to-object concepts
Object:
An object is an instance of a class that has state and behavior (for example, a dog is an object whose state is: color, name, symbol)
Actions are: wagging the tail, barking, eating and so on. (Concrete in kind)
Class: A class is an abstract description of a common thing that describes the behavior and state of a class of objects (abstract procedure, no reference data type actually exists)
A class is a collection of objects that are the concrete representation of a class
4: Object-oriented encapsulation
Encapsulation is actually the encapsulation attribute, let the outside world know this kind of state the less the better
5: Constructors
The constructor method is primarily used to create an instantiated object of the class, and the initialization of the created instantiation object can be done, declaring the format:
Constructor method modifier term List class name (method parameter list)
constructor modifier word list: Public, proteced, private
Class can be overloaded as well as normal methods
The characteristics of the construction method are:
The construction method name must match the class name
The construction method does not have any return value type, that is, no return value, the keyword void can not join, after joining is not the construction method, it becomes the common method.
Any class has a constructor method, and if there is no definition displayed, a default constructor is defined for the class that does not contain any parameters, if the definition displayed
The constructor, the system will not create a default constructor with no parameters.
/*defining a real-life student's class Student is a class, a student, a reference type Student is conceptually a definition that is not present in the real world syntax: Class modifier classes class name Exten The name of the DS parent object implements the name of the interface {class Body: Property and method Composition}*/ Public classstudent{//Properties//School Number intID; //member variables, instance variables, non-static variables//ID is the level of the object that must be accessible by existing objects and cannot be accessed directly using the class//name Public StaticString name ="Ming"; //Sexboolean sex; //Age intAge ; //AddressString address; //Method}/*creation and use of objects*/ Public classootest01{//Java Portal Public Static voidMain (string[] args) {//Creating Objects//Student is a student class//Stu is a local variable, which is a local variable when the student type, which is a reference type//Stu This local variable is called a reference, and the reference holds the memory address of the object in the heap. //indirect access to objects in the heap through referencesStudent stu =NewStudent (); //A class can also create multiple objectsStudent STU1 =NewStudent (); //Get Properties//name is a member variable, object-dependent, must have an existing object to access, and must use "reference."//If you do not want to close the object, but also need access: Changed permissions use static variables to declare staticSystem. out. println (Student.name); String N=Stu.name; System. out. println ("Student Name:"+N); System. out. println ("Student Number:"+stu.id); System. out. println ("Student Age:"+stu.age); System. out. println ("Student Sex:"+stu.sex); //Working with Objects//when you access member variables, you must use the reference. }}
Example 2:
/*Define a Ming type class = attribute + method; Object-oriented encapsulation refers to the following: 1. property privatization; 2. Publicly available setter and getter Set method and get method our method names should follow the specification. */ Public classming{//Properties//member variables, instance variables, non-static variables//private adornment data can only be accessed in this class Private intAge ; //Set Assignment Public voidSetage (intx) { //Conditional Judgment if(X <0|| X > Max) {System. out. println ("age is illegal!"); return; } Age=x; } //Get Get//the member method must use the reference. The way to access Public intGetage () {//member methods, instance methods, non-static methods returnAge ; }}/*Object-oriented encapsulation*/ Public classootest02{ Public Static voidMain (string[] args) {//create an object of type MingMing A =NewMing (); //Age of Reading Ming//System.out.println (a.age);//Error, because age is a private property//Assign Value//a.age = +;//Error, because age is a private property//assigning values using the Set methodA.setage (10000); //Read Get intAge =A.getage (); System. out. println ("Age of Ming:"+Age ); }}
Example 3:
/*Constructor 1. Syntax for constructing methods: "Modifier List of constructed methods" class name (method parameter list) {method body; } 2. The method name and class name of the constructor method are the same as 3. What is the function of the construction method? <1> Create objects <2> assign values to member variables (initialize member variables) 4. How the constructor method calls the new constructor method name (argument); Space Storage Object 5 is opened in the heap. If a class does not provide any construction methods, the system defaults to providing a non-parametric construction method if a class has manually provided the construction method, then the system will no longer provide any construction method; 6. When does a member variable assign a value? The member variable is assigned only when the method is called*/ Public classuser{//Properties//member variables, instance variables, non-static variablesString name; intAge ; //Define a construction method (the construction method forms an overload)User () {System. out. println ("I'm the first to construct a method"); Name="Ming"; Age= -; } User (inta) { Age=A; } User (String N) {name=N; } User (String N,inta) {Name=N; Age=A; }}/*Constructors (constructor, constructor, Constructor) syntax: Construction methods can be overloaded like normal methods*/ Public classconstructortest01{ Public Static voidMain (string[] args) {//Create a user type ObjectUser u=NewUser (); User U1=NewUser ( -); User U2=NewUser ("Brother Ming"); User U3=NewUser ("Ming", +); System. out. println (U.name); System. out. println (U.age); System. out. println (U1.name); System. out. println (U1.age); System. out. println (U2.name); System. out. println (U2.age); System. out. println (U3.name); System. out. println (U3.age); }}
Section eighth (object oriented, process oriented)