java classes and objects

java classes ...
1, Classes and objects
2, Class
3, Object
4, Combination type

java classes

1, Classes and objects

Class can be regarded as abstract feature extraction of concrete things. For example, people have the attributes of organs, walk, work, eat, drink, play and so on.

An object (instance) can be regarded as a concrete thing, that is, an instance of a class. For example, Xiaoming is an example of human beings.

2, Class

1. Definition of class

[modifier 1 Modifier 2...] class name{ ... }
  • Take a particle:
public class Student { // Properties: member variables int id; String name; String sex; int age; // Behavior: method void selfIntroduce() { System.out.printf("Hello, everyone. I'm a student. My name is%s,This year%d Years old, it's a%s Student, my student number is%d,Please take good care of me.\n", name, age, sex, id); } void study() { System.out.println("I am learning..."); } void speak(String s) { // s Is a local variable System.out.println(s); } }

2. Members of the class

(1) Properties: variables (fields)
  • Member variable: in a class, there is an initial value
  • Local variables: in methods
(2) Behavior: method

I think the methods are very familiar. I will not introduce them here.

(3) Constructor: construction method

The construction method is different from the common method. The construction method has no return value type, but it has return value, which is the reference address when creating a new object (New). Of course, there are also similarities, including parameter lists and overloading.

Every class has a constructor. When the constructor is not displayed, the system will give the class a constructor without parameters by default.

Construction method definition:

Class name (parameter list){ ... }
  • Take a plum:
public class Point { String name = null; float x; // x coordinate float y; // y coordinate Point() { // Default construction method } Point(float a, float b) { // Construction method overload x = a; y = b; } Point(float a, float b, String s) { // Construction method overload x = a; y = b; name = s; } void showPoint() { // Display point information if(name != null) System.out.printf("%s(%.2f, %.2f)\n", name, x, y); else System.out.printf("p(%.2f, %.2f)\n", x, y); } double distance(Point p) { //Calculate the distance between two points return Math.sqrt(Math.pow((p.x - x), 2) + Math.pow((p.y - y), 2)); } }

3, Object

1. Declaration object

Class name object name;

2. Create object

Object name = new class name (parameter list);

3. Parameter transfer

Basic data type: Value Passing refers to a specific number or string, so the content of the variable will not be modified.

Reference data type: refers to the reference value, that is, the address, so the content of the variable may be modified.

  • Here's a chestnut:
class E { public static void main(String args[]) { Point o = new Point(); // New object, using no parameter construction method o.x = 0; o.y = 0; o.name = "O"; Point a = new Point(3, 4, "A"); // New object, using refactoring construction method o.showPoint(); a.showPoint(); System.out.printf("d(OA) = %.2f\n", a.distance(o)); } }

4, Combination type

——Class A declares object B of class B as its member variable

1. Class composite object

When declaring objects of other classes as their own member variables in a class, either create a new object without the new keyword, or declare and create in the same statement.

  • Another Chestnut:
class Circle { Point o; // Right // o = new Point(0, 0, "O"); // Error // Point oo = new Point(0, 0, "O");// Right float r; // Right // r = 5; // Error // float rr = 5; // Right Circle() { // Default construction method } Circle(Point O, float R) {// Overload construction method o = O; r = R; } void getCenterPoint() { // Get center information o.showPoint(); } double area() { // Calculate circle area return Math.PI*Math.pow(r, 2); } }
class E { public static void main(String args[]) { Point o = new Point(0, 0, "O"); // Newly build Point Objects, using refactored construction methods Circle c = new Circle(o, 8); // Newly build Circle Objects, using overloaded construction methods c.getCenterPoint(); // Get center information System.out.printf("Area: %.2f", c.area()); // Calculation circle o Area of } }

2. Some rules of combination type

  • Class methods can only operate on class variables
  • Instance method can operate class variable and instance variable
  • Class can only call class methods and class variables
  • Object can call class method, instance method, class variable and instance variable

4 May 2020, 10:53 | Views: 3014

Add new comment

For adding a comment, please log in
or create account

0 comments