java study notes 5: object-oriented thinking, classes and object member variables and local variables, the package

First, the object-oriented thinking
when demand for a single, or simple, we go step by step operation is no problem, and efficiency is also pricey. With the demand can change the function of the increase was found to face a lot of trouble every step. Then began to think, these steps can function to encapsulation and, when the package according to different functions for different packages, packaged together similar functions. Such structures would clear a lot. When used to find the corresponding class on it. This is the object-oriented thinking.
Features:
1, is a more in line with our thinking habits of thought of
2, can be complex things simple
3, we will become a commander from the executor, the role of the conversion occurs
two, with the object class
1, class is our Java smallest unit, he embodies the idea of a packaged, the package member variables (attributes) and member methods (functions), is a collection of related attributes and behavior of a group. Object is a concrete manifestation of such things.
For example:
class: student
objects: squad is an object (a concrete object)
2, member variables and member methods
member variables: definitions and variables are the same, but the location has changed. Outside the class, method.
Member methods: definitions and methods are the same, but get rid of the static.
3, create a format
objects: class name = new class name object name ();
member variables: the object name variable name;
member methods: Method name object name;
4, for example
to complete a test and Student class

public class MyTest {
    public static void main(String[] args) {
      Student student=new Student();
      //给成员变量赋值
      student.name="张三";
      student.age=20;
      student.sex='男';
      System.out.println(student.name);
      System.out.println(student.age);
      System.out.println(student.sex);
      student.eat();
      student.sleep();
      student.palyGame();
   }
}
class Student { //定义了一个学生类
    //定义成员变量
    String name;
    int age;
    char sex;
    //成员方法
    public void eat() {
        System.out.println("吃饭");
    }
    public void sleep() {
        System.out.println("睡觉");
    }
    public void palyGame() {
        System.out.println("打豆豆");
    }

}
输出:张三
      20
      男
      吃饭
      睡觉
      打豆豆  

Note : generally written two classes in a java file: A basic class (Student), a test class (MyTest). A java class file can be defined in two, but only one type is public of
three differences, member variables and local variables
1, different positions in the class
member variable: an outer class methods
local variables: the method defined in on or method declarations
2, different location in memory of
member variables: the heap
of local variables: stack memory
3, the life cycle of different
member variables: with the creation of the object exists, the object disappear with the disappearance of
local variables : with the call method exists, along with the method call is completed and the disappearance of
4, initialization values of different
member variables: initialized with a default value of
local variables: no default initialization values, you must define the assignment before you can use.
Note : Local variables and member variables can name the same name, when used in the method, using the principle of proximity.
Fourth, the package
1, package refers to the hidden object's properties and implementation details, only the external provision of public access.
2, the benefits package
①, hide implementation details, provide a common access method
②, improve the reusability of code
③, improve security.
3, the principle
will not need to provide external content are hidden. Hide the property to provide public access to its methods.
4, private key
①, is a privilege modifier
②, you can modify member variables and member methods
③, modified by its members can only be accessed in this category
5, this keyword
①, Why should there be this: When our local variables and member variables the same time, if we do not use this keyword, it will lead to one problem: the problem is that local variables hiding member variables.
②, Features: It is an object of the current class references. Simple mind, it represents an object of the current class. Who calls this method, so this inside the method represents who
③, this application scenarios: hidden member variables to address local variables.
6, for example: establishment of a student using the encapsulation class (and this with the private key)

public class MyTest {
    public static void main(String[] args) {
        Student student = new Student();
        //调用set方法给成员变量设置值
        student.setName("张三");
        student.setAge(18);
        String name = student.getName();
        int age = student.getAge();
        System.out.println("姓名: "+name+"---年龄:"+age);
        }
     }
        public class Student {
         //private 权限修饰符 私有的,可以修饰成员变量和成员方法,被修饰的成员只能在本类中访问
         //私有化成员变量
        private String name;
        private int age;
        //提供公共的get set 方法
        public void setName(String name){
           this.name=name;
        }
        public void setAge(int age){
          this.age=age;
        }  
        public String getName(){
          return name;
        }
        public int getAge(){
         return age;
        }
    }
    输出:
    姓名:张三---年龄:18
Published 24 original articles · won praise 11 · views 2063

Guess you like

Origin blog.csdn.net/weixin_43791069/article/details/84949061