Java learning notes-Object-oriented Thinking, java learning notes

Source: Internet
Author: User

Java learning notes-Object-oriented Thinking, java learning notes

1. unchangeable class generation object and variable range

2. Use of the keyword "this"

3. Use class abstraction to create software

4. Create a class through the relational model

5. Use object-oriented examples to design programs and follow the class design guidelines.

I have learned how to define the class that has already created an object, and how to use the class through Java APIs.

 

1. Immutable classes and objects

Normally, we use classes to generate an object and allow future changes to the object content, but sometimes it is required that the content cannot be changed. For example, when we record the student archive or resume, some archives, such as the birth date, will not be changed once confirmed, but the education level, work experience will change as individuals grow. Some Classes in Java will not change. The content in the String class mentioned above will change generally. When you change it, refer to the String variable pointing to this value. We only need to private the data zone and then achieve our goal by failing to change the information in public methods. Variables in the data area of the class can only be declared once, but different program block variables in the method can be declared many times.

Example

public class circle{ private int x = 0; private int y = 0; public circle(){}    public void p(){    int x = 1;    system.out.println("x = " +x);    system.out.println("y = " +y);  } }

By comparing the output values of x and y, we can see that the value of x in the p method has changed, but the value of y has not changed, that is, the value of x defined in the method, its scope is in Method p. To avoid confusion and errors, do not use local or static variables in the method, except for passing the parameter location using methods.

2. Use of the keyword "this"

This is a reference name. It points to the object itself, and the most direct is to reference the hidden data zone of the class itself.

  

public class Circle{  int x = 10;  static double k = 11;   void setX(int x){      this.x = x;   }   static void setK(double k){      Circle.k= k;   }}

The keyword "this" is used to obtain the object and then call the instance. this. x = x is to assign the value of x to the data zone of the raised object. If we change the usage of this to this (abc), we can understand it as the constructor of this class.

3. Use class abstraction to create software

Class abstraction and encapsulation. Class abstraction is to use a class and implement Part of the content in the class. To create this class, you must let the user know how it is used. methods and data can be included in the external and class. The idea of class implementation. For the client, the class is like a black box. When used, the class is abstracted and the signature of the method and content is obtained. The client uses abstract classes. In life, computers are divided into many parts, including CPU, memory, hard disk, and motherboard. Each part is an object, and each part has attributes and Methods. To integrate a whole computer, we must know how each part is used and how they interact, internal things have been encapsulated. We don't need to know how each trigger and the latch works. We only need to know the overall module functions.

That is to say, 1 Development class and usage class are two independent tasks.

2. You can skip complex classes and then learn the entire project.

3. You can use classes to better understand how to implement them.

Object-oriented thinking:

Public static double gerBMI (double weight, double height)

This method can calculate our body's BMI value, which is still very limited, because after we get this information, we use it as a metric to measure the health of a person in our daily use, this information is connected to other information of this person. You can create a class as follows:

  

String name;int age;double weight;double height;BMI(name: String , age: int ,weight:double, height: double)getBMI       

An object can also contain another object, as shown in the above BMI example. In BMI, there are many types of relationships among the strings generated by the String class, for example

Student, name, and home address. A student corresponds to a name, but two students may have the same address. This address is shared by the two students, that is, the class is created through the relational model.

public class Name{    ...}public class Student{   private Name name;      private Address address;  }public class Address{  ...   }
4. Create a class through the relational model

Example: design a Course class

Data zone: courseName: String, students []: String, numberOfStudent: int

Method Area: constructor Course (courseName: String), getCourseName (): String, addStudent (student: String): void, dropStudent (student: String): void, getStudents (): string [], getNumberOfStudents (): int

In this Course class, you can see the shadow of the data structure, a set of data, and a set of relationships on this set of data, a bit like a linear table.

Student. java

  

public class student {    private String Name;    public student(String na){        this.Name = na;    }    public String getName()    {        return Name;    }}

Course. java

public class Course {    private String CourseName;    private student[] stu;    private int numberOfStudent;        public Course(String courseNa){        CourseName = courseNa;        numberOfStudent = 0;        stu= new student[20];    }        public String getCourseName(){        return CourseName;    }    public void addStudent(student s){        int i = numberOfStudent;        i++;        stu[i] = s;            numberOfStudent++;    }    public void DropStudent(student s){        numberOfStudent --;    }    public String GetStudent(int i){        return stu[i].getName();    }    public int  getNumberOfStudent(){        return numberOfStudent;    }}

Main. java

public class Main {    public static void main(String[] args){                Course cou = new Course("math");        student stu1 = new student("xiao ming");        student stu2 = new student("Da ming");        cou.addStudent(stu1);        cou.addStudent(stu2);        System.out.println("The Course Name is "+ cou.getCourseName());        System.out.println("The number of student is "+ cou.getNumberOfStudent());        System.out.println("The List :\n"+cou.GetStudent(1));        System.out.println(cou.GetStudent(2));    }    }

 

5. Use object-oriented examples to design programs and follow the class design guidelines.

An object should describe a single whole, and class operations should be logically consistent. For example, you can add students to a class, but do not add employees and students to the same class. The strings, StringBuilder, and StringBuffer learned previously are used to solve the problem of strings, string is used to process unchanged strings. StringBuilder is used to create variable strings. StringBuffer can be similar to StringBuilder, but StringBuffer can contain the synchronized Method to update strings.

High Cohesion, low coupling, consistent and coherent.

From Encyclopedia:

Coupling: Also known as inter-block connections. A measure of the closeness between modules in the software system structure. The closer the relationship between modules, the stronger the coupling, and the poorer the module independence. The coupling between modules depends on the complexity of Inter-module interfaces, call methods, and transmitted information.

Cohesion: Also known as intra-block contact. A measurement of the module's functional strength, that is, a measurement of the closeness of each element in a module. If the elements in a module (between language names and between program segments) are closely related, the higher the cohesion. High cohesion refers to a software module that consists of highly correlated Code and is responsible for only one task, that is, a single responsibility principle. Coupling: a measurement of the degree of interconnection between different modules in a software architecture. For low coupling, the superficial understanding is: a complete system, between modules, as far as possible to make it independent. That is to say, let each module complete a specific sub-function as independently as possible. The interfaces between modules are as few as possible and as simple as possible. If the relationship between two modules is complex, it is best to consider further module division first. This facilitates modification and combination.

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.