This is the 11th day of my participation in the August More Text Challenge. For details, see:August is more challenging

Java Learning Notes series — Sun Bujian 1208

【 Detail + super basic 】Java- Learning Notes 01

【 Detail + super basic 】Java- Study Notes 02

【 Detail + super basic 】Java- Study Notes 03

【 Detail + super basic 】Java- Study Notes 04

【 detail + super basic 】Java- Study Notes 05

【 Detail + super basic 】Java- Study Notes 06

Ongoing updates….

Object oriented programming

Object-oriented thinking

Java language is a kind of object-oriented programming language, and object-oriented thought is a kind of programming thought, we use Java language to design and develop computer programs under the guidance of object-oriented thought. The object here refers to all things in reality, each of which has its own properties and behaviors. The object oriented idea is the design idea that abstracts the attribute and behavior characteristics of things and describes them as computer events in the process of computer programming, referring to things in reality. It is different from process-oriented thinking in that it emphasizes the implementation of functions by calling the behavior of objects, rather than implementing them step by step.

Let’s do the laundry.

Process oriented: Take the clothes off and put them in a basin -> put some washing powder, add some water -> soak for 10 minutes -> knead and wash the clothes -> wring and drying

Object oriented: Take clothes off -> use automatic washing machine -> throw clothes -> button -> air

Classes and objects

The understanding of the class

A class is an abstraction of a class of things with common properties and behaviors in real life

A class is the data type of an object. A class is a collection of objects that have the same properties and behavior

Simple understanding: class is a description of real things

The composition of the class

Attribute: a characteristic of something, such as a mobile phone (brand, price, processor)

Behavior: An action that something can perform, such as a cell phone (calling, texting, surfing the Internet)

Relationships between classes and objects

Class: A class is an abstraction of a class of things in real life that have common properties and behaviors

Object: it is a real entity that can be seen and touched. All things that exist objectively are objects, so we often say that everything is an object.

Simple understanding: class is a description of things, objects for the concrete existence of things

The definition of a class

The composition of a class consists of two parts: attributes and behaviors

Properties: represented in a class by member variables (variables outside a class method)

Behavior: Represented by member methods in the class (dropping the static keyword in comparison to previous methods)

Class definition steps: ① define class ② write class member variables ③ write class member methods

Code:

Public class class name {// member variable type 1; The data type of variable 2 is variable 2; ... // Member method method 1; Methods 2; } public class Phone {// String brand; int price; Public void call() {system.out.println (" call "); } public void sendMessage() {system.out.println (" send a message "); }}Copy the code

Use of objects

Create object format:

Class name Object name = new class name ();

The format of the calling member:

Object name. Member variable

Object name. Member method ();

Code:

Object name = new class name (); Phone p = new Phone(); Use object 1: Use member variable format: object name. Example variable name: P. rand 2: Use member method format: object name. P.call () '*/ public class PhoneDemo {public static void main(String[] args) {// Create object Phone p = new Phone(); // Use member variable p.rand = "millet "; p.price = 1999; System.out.println(p.brand); System.out.println(p.price); // Use the member method p.call(); p.sendMessage(); }}Copy the code

Exercise: Student objects

Requirements: first define a student class, then define a student test class, member variables: name, age; Member methods: Study, do homework… Use of member variables and methods in the student test class through objects

Code:

Class Student {// member variable String name; int age; Public void study() {system.out.println (" study hard "); } public void doHomework() {system.out.println (); }} public class StudentDemo {public static void main(String[] args) {// Create object Student s = new Student(); // Use object s.name = "sun Bujian "; s.age = 18; System.out.println(s.name + "," + s.age); s.study(); s.doHomework(); }}Copy the code

Guide: Indicates where the classes to be used are located.

Import Package name. The class name;

The import demo01. Student;

If the class is in the same package as the current class, you can omit the package derivation statement.

Member variables and local variables

Different positions in the class:

Member variables (outside a method in a class) Local variables (inside a method or on a method declaration)

Different locations in memory: member variables (heap memory) Local variables (stack memory)

Different scopes: member variables (in the class) local variables (in the method)

Different life cycles:

Member variables (exist as the object exists and disappear as the object disappears) Local variables (exist as the method is called and disappear when the method is called)

Different initialization values: member variables (which have default initialization values) local variables (which have no default initialization values and must be defined before being assigned)

Guess the number of small game

A Scanner,

1. Idea import java.util.Scanner;

2. To create

Class name Object name = new class name ();

Scanner sc =new Scanner(System.in);

System.in stands for typing from the keyboard

Use 3.

Object name member method name ()

Int num = sc.nextint ();

Gets a String typed on the keyboard: String STR =sc.next();

Second, the Random

1. Idea import java.utilranom ()

2. To create

Class name Object name = new class name ();

Random r=new Random();

Use 3.

Int num= r.int (); int num= r.int ()

Int num= r.int (a) // to generate a random number in [0,a]

The code is as follows:

Package b. import java.util.Random; public class random01 { public static void main(String[] args) { Random r =new Random(); int num=r.nextInt(); System.out.println(" random number is "+num); }} package random name; import java.util.Random; public class random01 { public static void main(String[] args) { Random r =new Random(); int num=r.nextInt(5); String [] name = new String [] {" lyu3 bu4 ", "guan yu", "zhang fei", "zhaoyun", "sun cannot"}; Println (" name[num] +name[num] "); }}Copy the code

Third, guess the number of small game

Ideas:

1. First, a Random number needs to be generated, and once generated no longer changes, using the Random nextInt method

2. We need to enter numbers on the keyboard, using Sanner’s nextInt method

3. Judge the size relationship between the two numbers and give feedback

The code is as follows:

packageLittle practice;import java.util.Random;
import java.util.Scanner;

public class caishuzi{
    public static void main(String[] args) {
        	Random r = new Random();
        int randomNUM = r.nextInt(100) + 1;
        	Scanner sc = new Scanner(System.in);
        while (true){
            		System.out.println("Please enter your guess:");
            		int guessNUM = sc.nextInt();// The number you guessed
            	if (guessNUM > randomNUM) {
                	System.out.println("Too big, please try again.");
             	} 
            	else if (guessNUM < randomNUM) {
                	System.out.println("Too small, please try again.");
               	} 
           		else {
                 	System.out.println("Congratulations, you got it.");
                 	break; }}}}Copy the code

Welcome to subscribe column to invite you to drink a cup of Java, hope to bring convenience to friends in need, but also hope to get everyone’s attention and support.