java learning Day09-object-oriented (on)

Object-oriented

Process Oriented (pop)

Analyze the specific implementation steps to achieve step by step

Object-oriented (oop->object-oriented programming)

Think and solve problems in a classified way, analyze and classify things as a whole

Case: Put the elephant in the refrigerator

Process-oriented thinking

1. Open the refrigerator door

2. Pack the elephant in

3. Close the refrigerator door

Object-oriented thinking

Design category:

1. Human (load stuff)

2. Elephant

3. Refrigerator (to store things)

4.Door (open door, close door)

Object-oriented is also inseparable from process-oriented, and the implementation of specific details is still process-oriented

Java class

A class is a template, which defines the common attributes and characteristics of a class object (object), and creates a file (.java class) to record this information.

Class definition:

​ The structure of the class:

​ Variables: description of the properties of things;

​ Method: the behavior of things;

​ Construction method: used to create objects;

​ Inner class: the class declared in the class body;

​ Block: A code block without a name

​ Design category:

​ 1. Discover the class grammar

​ 2.Define the attributes, characteristics, and variables of the class

​ 3. Define the behavior, function, and method of the class

/**
 * 发现类
 * [访问权限修饰符] [修饰符] class(定义类) 类名 {
 *      变量:事物属性的描述;
 *      方法:事物的行为;(可以做的事情)
 *      构造方法:用于创建对象;
 *       内部类: 即在类体中声明的类。
 *       块:一段没有名称的代码块
 * }
 * 访问修饰符有两种 public 或者 不写
 * 修饰符: final , abstract
 * 类名: 类名首字母大写,见名知意,驼峰表示
 */
public class People {
    
    
    /**
     * 发现共有属性
     * 成员变量定义
     * [访问权限修饰符] [修饰符] type 变量名;
     */
    String name;
    String sex;
    int age;

   
    /**
     成员方法
     人有睡觉吃饭学习等行为
     [访问权限修饰符] [修饰符]/void  方法名(参数类型 参数名){
     sout();
     [return 返回值;]
     }
     */
    /*睡觉的方法*/
    public void Sleep() {
    
    
        System.out.println(name + "说费觉觉");
        return;
    }

    /* 吃饭的方法*/
    public void Eat() {
    
    
        System.out.println(name + "说开饭了,冲冲冲");
    }

    /*学习的方法*/
    public void Study() {
    
    
        System.out.println(name + "说一秒不学习,浑身难受");
    }
}

Java objects

An object is an instance of a class, which is an actual instance created in memory by the class as a template.

An object is actually an instance of concrete existence, and creating an object is to instantiate an object

/*
          类定义好后,是一个文件,但不能使用
          以类为模板创建对象,对象是实例,视具体存在的,可以被使用
          People cui = new People();
                       new + People 类的构造方法,在内存中创建一个具体的对象
                      People cui 以People类为类型声明一个变量cui
                       = 让cui变量 指向(引用)内存中的对象
           cui.name = "张三";
           使用cui变量 . 调用对象中的属性(变量)\行为(方法)
         */
        People zhang = new People();
        zhang.name = "张三";
        zhang.age = 21;
        zhang.sex = "男";
        System.out.println("姓名:" + zhang.name + "\t性别:" + zhang.sex + "\t年龄:" + zhang.age);
        zhang.Eat();
        zhang.Sleep();
        zhang.Study();

Variable classification

Divided by data type:

​ Basic types: 8 types

​ Reference data type: class as type, array

Classified by location:

Member variables: properties of things

Defined in the class, outside the method body

​ All data types supported in java can be used

​ It is not necessary to initialize and assign values. When creating an object, the virtual machine assigns values ​​by default.

​ String :null char:" " int:0 Boolean:false

​ Member variables can be accessed in member methods

Local variables: the behavior of things

Defined in methods, constructors, and code blocks, supporting all data types

​ Initialize the assignment before use, and the variable will be destroyed after the method ends

Method classification

Member method

    	/*
        成员方法
        [访问权限修饰符] [修饰符] [返回值类型]/void 方法名(参数类型  参数名){
             语句块
             [return  值]
        }
        */	
	public void Sleep() {
    
    
        System.out.println(name + "说费觉觉");
        return;
    }
	//int day,String deep  方法参数也是局部变量
	public void Play(int day,String deep){
    
    
        System.out.println(name+deep+"使劲玩");
    }
	 People zhang = new People();
	 //成员方法必须通过对象调用
     zhang.Sleep();
	 zhang.Play(22222,"哈哈哈哈");

Guess you like

Origin blog.csdn.net/XiaoFanMi/article/details/110095996