Object oriented Java

Keywords: Java

Object oriented Java

6.1 process oriented and object-oriented

What is object-oriented? What's the difference between object-oriented and process oriented?

Process oriented is to analyze the steps needed to implement the requirements, and implement these steps step by step through functions, and then call them in turn. Object oriented is that we don't need to implement so many actions step by step, we just need to command others to do so, and a program can't be completely object-oriented, but a program can be completely process oriented. Let's take a simple example: the boss asked manager Wang to recruit people. From the perspective of the boss, it's object-oriented to command Lao Wang. From the perspective of Lao Wang, it's a day to brush your teeth and wash your face, get dressed and go downstairs, have breakfast, go to work and take transportation, go to the company to start work, interview candidates, have lunch at noon.... Come home from work. If from the span of a whole day, it is process oriented.

Our common object-oriented operations:

 System.out.println("Here we go.~");

The system comes here. Output. Output at the console.

 Scanner scanner=new Scanner(System.in);

Call me the man who scanned the console. The name of the man is scanner.

    int a=scanner.nextInt();

You have this person read an integer from the console and give the variable a.

    Random random=new Random();

Call me the person who is responsible for generating random numbers. The name of this person is random.

    int a=random.nextInt();

Let this person generate a random integer for me to give variable a.

6.2 objects and classes

In the previous study, we know that every program has a class, and there is a main function in the class. Now we study object-oriented, and generally, a program only contains one class. If we need another class, we will recreate a code edit box.

For example, we create a Point class:

public class Point {
     double x ;
	 double y ;
}

This means that we created a Point class with two variables x and y. Coordinates representing a Point.

We can get the x and y coordinates of this class through the main function, but first we will create an object for this class.

public class Main {
	public static void main(String[] args) {
		Point p1 = new Point();
        p1.x = 1,p1.y=1;
        System.out.println(p1.x);
        System.out.println(p1.y);

	}

}

Here we create an object p1 of the Point class, and then we directly output the x and y coordinates of this Point.

public class Point {
     double x ;
	 double y ;
     	public double distance(Point other) {
		 return Math.hypot(x-other.x,y-other.y);
	}
}

We define a function in this class to find the distance between two points. We call the function in the main function as follows:

public class Main {
	public static void main(String[] args) {
		Point p1 = new Point ();
		Point p2 = new Point ();
		p1.x=1;
        p1.y=1;
        p2.x=2;
        p2.y=2;
		System.out.println(p1.diatance(p2));

	}

}

So we can get the distance between p1 and p2.


6.3 encapsulation and private keywords

We found that in other classes, we can easily modify any function in the class we defined, which is very unsafe, so we need to privatize the variables in the class. Before defining variables, we add the keyword private, and we will not access the variables in other classes. Only this class can be accessed. For example:

public class Point {
	private double x;
	private double y;
	public double distance(Point other) {
		 return Math.hypot(x-other.x,y-other.y);
	}

}

But in doing so we ignored what if we needed to visit him? We can neither access him nor modify him. When we need to change it, we can only change and access it in this function, so we can do this:

public class Point {
	private double x;
	private double y;
	public double distance(Point other) {
		 return Math.hypot(x-other.x,y-other.y);
	}
	public double getX() {
		return this.x;
	}
	public double getY() {
		return this.y;
	}
}

In this way, we can call functions in this class in other functions to access the variable. We can also add modified functions to determine whether the modified values meet the standards, and then judge whether to modify them.

6.4 difference between member variable and local variable


1. Storage location
Local variables are stored in the space of the function in the stack memory
Member variables are stored in the object's own space in heap memory
2. Life cycle
Local variables exist with the function stack, and disappear with the function stack
Member variables exist as objects are created and disappear as objects are destroyed
3. Scope of visit
The access scope of local variables is only in the function
The access scope of member variables is in the current class
4. Initialization value
Local variables must be initialized before they can be called
Member variables are initialized by default when they are created

6.5 constructor

We can only assign values to member variables after the object is created. Some members have values (excluding default initialization) before the object is created, and some members have values only after the object is created. So there is a problem. How to assign values to our member variables before the object is created? We need a function constructor. The function constructor is as follows:

public class Point {
	private double x;
	private double y;
	public double distance(Point other) {
		 return Math.hypot(x-other.x,y-other.y);
	}
	public Point() {
		
	}
	public Point (double x ,double y) {
		this.x = x;
		this.y = y;
	}
	public void setX(double x) {
		this.x = x;
	}
	public void setY(double y){
	        this.y=y;
	    }
	public double getX() {
		return this.x;
	}
	public double getY() {
		return this.y;
	}
}

Our main function is constructed as follows:

public class Main {
	public static void main(String[] args) {
		Point p1 = new Point (0,10);
		Point p2 = new Point (10,0);
		Point p3 = new Point (0,0);

	}

}

In this way, we have successfully constructed three Point objects, and given initial values directly when creating, so we do not need to assign values after creating the objects.

If we need to define a triangle class, which contains three points, we need to find the area and perimeter of the triangle formed by these three points? It's simple:

public class Main {
	public static void main(String[] args) {
		Point p1 = new Point (0,10);
		Point p2 = new Point (10,0);
		Point p3 = new Point (0,0);
		Traight a = new Traight(p1,p2,p3);
		System.out.println(a.getArea());
		System.out.println(a.getDistance());
		p1.setX(0);
		p1.setY(5);
		System.out.println(p1.getX());
		System.out.println(p1.getY());
		System.out.println(a.getArea());
		System.out.println(a.getDistance());

	}

}
public class Traight {
	private Point p1;
	private Point p2;
	private Point p3;
	public Traight() {
		
	}
	public Traight(Point p1,Point p2,Point p3) {
		this.p1 = p1;
		this.p2 = p2;
		this.p3 = p3;
	}
	public double getDistance() {
		return p1.distance(p2)+p2.distance(p3)+p1.distance(p3);
	}
	public double getArea() {
		double side1 = p1.distance(p2);
		double side2 = p1.distance(p3);
		double side3 = p2.distance(p3);
		double s = (side1+side2+side3)/2;
		return Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
	}
}
public class Point {
	private double x;
	private double y;
	public double distance(Point other) {
		 return Math.hypot(x-other.x,y-other.y);
	}
	public Point() {
		
	}
	public Point (double x ,double y) {
		this.x = x;
		this.y = y;
	}
	public void setX(double x) {
		this.x = x;
	}
	public void setY(double y){
	        this.y=y;
	    }
	public double getX() {
		return this.x;
	}
	public double getY() {
		return this.y;
	}
}

We have three points in the triangle class. In the main function, we create three points, and then construct a triangle with points. Then we can calculate.

Published 8 original articles, won praise 8, visited 796
Private letter follow

Posted by 121212 on Thu, 27 Feb 2020 01:42:37 -0800