Day06 Java Basics Learning Notes

Source: Internet
Author: User

Object oriented

When the requirements are single, or simple, we are working step-by-step without problems, and the efficiency is quite high. With the need to change, the increase in functionality, found that the need to face each step is very troublesome, then began to improve, can be used to encapsulate these steps and functions, packaging, according to different functions, different packages, functions similar, or related to the encapsulation of the relationship, so the structure is much clearer. When you use it, find the corresponding class. This is the idea of object-oriented.

For example, there are three people, each with their own function: using pseudo-code to express

    张三{       Public void  部署程序(){            System.out.println("部署java程序");       }    }    李四{       Public  void 写代码(){            System.out.println("写java代码")       }    }    老板{        Public static void main(String[] args){            李四.写代码();            张三.部署程序 ();            ...        }    }
This conclusion can be drawn from the above overview:
    • Process oriented: The emphasis is on the process, each process and implementation of their own steps to implement, consider more things

Example: First learn Java syntax, then write Java programs, and then compile, run each of the details of the implementation of their own;

    • Object-oriented: Emphasis on objects, when certain functions are required, to find objects with such functions, and then by the object to invoke the function

Example: Know Zhang three will write Java program, to find Zhang San, let Zhang San write program, the results to me only need to get someone to work, and then wait for the results.

The characteristics of object-oriented thinking
    • is a thought that is more in line with our thought habits.
    • Can make things more simplistic, do not have to consider every detail of things, only in the macro grasp
    • And turned us from the executor to the conductor.
Classes and objects
    • Object: A complex that can be understood as having some function
    • Class: Abstraction of the same thing that abstracts their common attributes and behavior
Object-Oriented Programming process: In fact, it is the continuous writing of various "classes", and then use the class to create objects, and then use objects, command object completion function
    • Classes in Java:

There are member variables that describe the properties of this class, and there are member methods that simulate the behavior of the class.

Member variables--Properties of the class

Member Method-The function of the class

Summarize classes and objects in Java
    • Class: The combination of a set of related variables and methods is an abstract concept
    • Object: is a class instantiation created, a concrete manifestation of the concept of "class"
Example
    • Student (Class): Is a class, it is an abstract concept, there is a study number, name, age and other properties, learning, writing, and other behaviors belong to the students, not refers to an individual
    • Zhang San (object): is a student class of a specific object, he is a student, with students ' attributes and behavior
The definition of a class defines a student class
    public class Student {        //成员变量,学生类的属性        public String name;         //成员变量是有默认值的,引用数据类型,默认为null        public int age;     //0        public String address;        //成员方法,学生类的行为        public void study(){            System.out.println("study");        }        public void eat(){            System.out.println("eat");        }        public void sleep(){            System.out.println("sleep");        }    }
Define a phone class
    public class Phone {        //成员变量,手机类的属性        public String brand;      //成员变量是有默认值的,引用数据类型,默认为null        public int price;        public String color;        //成员方法,手机类的功能        public void call(String name){            System.out.println("给"+name+"打电话");        }        public void sendMessage(){            System.out.println("发短信");        }    }
Use of the class to create the object
    类名 对象名 = new 类名();
Using variables or methods of an object
    对象名.成员变量;    对象名.成员方法;
Define a test class to test the above student class
    ////定义测试类,这个类主要是用来存放main方法,让JVM执行,不是用来创建对象用的    import home.Student;        public class StudentTest {        public static void main(String[] args) {            //类名 对象名 = new 类名();            Student stu = new Student();             //输出成员变量值            System.out.println(stu.name);            System.out.println(stu.age);            System.out.println(stu.address);            //给成员变量赋值            stu.name = "张三";            stu.age = 24;            stu.address = "北京昌平";            //重新输出成员变量值            System.out.println(stu.name+stu.age+stu.address);            //调用方法   对象名.方法名            stu.study();            stu.eat();            stu.sleep();        }    }
Define a test class to test the above phone class
    public class PhoneTest {        public static void main(String[] args) {            //创建手机对象            //类名 对象名 = new 类名();            Phone p = new Phone();  //直接输出成员变量值            System.out.println(p.brand+" "+p.price+" "+p.color);            //给成员变量赋值            p.brand = "apple";            p.color = "gold";            p.price = 5000;            System.out.println(p.brand+" "+p.price+" "+p.color);  //再次输出            //调用方法            p.call("张三");            p.sendMessage();        }    }
Memory diagram and initialization process for 1 phone objects

Phone p = new phone ();

Phone p This step is a space in the stack space, also known as pressure stack; new Phone () This step loads the Phone.class file while opening up memory space in the heap;

SOP (P.brand + "+ p.price +" + p.color) This step is to display the initialization value of the instance variable;

P.brand = "Samsung"; p.price = 3999; P.color= "White" These three steps are to assign values to the instance variables and overwrite the initial values;

P.call ("Jobs"); P.sendmessage ();p. PlayGame (); These three steps really call the member method;

First the main method is defined, the main method is stored in the static area of the method area, and the phone is loaded. class file to create the object phone p to press the stack, the new phone in the heap to open up space and the member variables to initialize the value, to re-assign the member variables, and then through the main method of the object p to invoke the member method, and the compression stack, the method executes after the stack.

instance variables and local variables

Defined:

    • Instance variable: an instance object belonging to a class that has no static adornments, and the member variables currently exposed are instance variables

    • Local variables: Variables that are part of a method, only valid within a method

Difference:

Different positions in the class

    • Instance variables are in a class and are defined outside of the method

    • Within a local variable method or on a method declaration (formal parameter) definition

In-memory locations are different

    • Instance variable heap memory (belongs to object)

    • Inside the local variable stack (belongs to method)

Different life cycle

    • Instance variables exist as the object exists and disappear as the object disappears

    • Local variables exist with the invocation of the method and disappear as the call to the method finishes

Different initialization values

    • Instance variable has default initialization value (0/false/null)

    • A local variable has no default initialization value and must be defined before it can be used.

Formal parameters
    • The basic type as the formal parameter: The value of the argument is actually a copy of the value of the actual parameter to the parameter, no matter how the parameter changes, will not affect the value of the argument
    • A reference type is a formal parameter: the instance object or array to which the argument is passed, that is, the reference to the argument (the address value), the formal parameter and the argument have the same reference, so the changes to the reference are immediately visible from the argument.

      public class Demo1{    public int sum(int a ,int b){        return a+b;    }    public void show(){        System.out.println("学习");    }}测试类public class DemoTest {    public static void main(String[] args) {        Demo1 d = new Demo1();        System.out.println(d.sum(5, 6));        d.show();    }}
Encapsulation Package Overview
    • Encapsulation is one of the three important features of object-oriented programming, and the remaining two are polymorphism and inheritance.

    • Encapsulation refers to the properties and implementation details of hidden objects, does not allow external direct access to the object's internal information, but through the method provided by the class to achieve access to internal information.

In order to achieve a good package, the following two aspects need to be considered
    • Hides the object's instance variables and implementation details, and does not allow direct access to the outside world.

    • Exposing methods to control the safe access and operation of these member variables

      封装练习public class StudentDemo1 {    private String name;  //封装名字    private int age;    //封装年龄    public String getName(){  //通过类内部方法获取变量        return name;    }    public void setName(String name){  //给变量进行赋值        this.name = name;    }    public int getAge(){        return age;    }    public void setAge(int age){        this.age = age;    }}测试类public class StudentDemo1Test {    public static void main(String[] args) {        StudentDemo1 sd1 = new StudentDemo1(); //创建对象        sd1.setName("张三");    //给变量赋值        System.out.println(sd1.getName());  //通过引用方法对变量进行打印        sd1.setAge(25);        System.out.println(sd1.getAge());    }}
Private keyword
    • is a permission modifier.

    • Members can be decorated (member variables and member methods)

    • Members that are modified by private can only be accessed in this class.

The object-oriented exercise defines a class demo, which defines a method that asks two data and defines a test class, test, for testing.
    public class Demo {        public int num1;        public int num2;        public int getSum1(){            return num1+num2;        }    }    测试类    public class DemoTest {        public static void main(String[] args) {            Demo d = new Demo();            d.num1 = 10;            d.num2 = 20;            System.out.println(d.getSum1());        }    }
Define a rectangular class rectangle, define a method for calculating perimeter girth and area areas, and then define a test class Test2 to test.
    public class Rectangle {        public int a; //长方形的长        public int b;//长方形的宽        //定义方法求边长        public int girth(){            return (a+b)*2;        }        //定义方法求面积        public int area(){            return a*b;        }    }    测试类    import java.util.Scanner;  //导包  键盘输入        public class RectangleTest {            public static void main(String[] args) {                Scanner input = new Scanner(System.in);                Rectangle rect = new Rectangle();  //创建对象                System.out.println("请输入长方形的长:");                int a = input.nextInt();                System.out.println("请输入长方形的宽:");                int b = input.nextInt();                rect.a = a;    //给成员变量赋值                rect.b = b;                System.out.println("长方形的周长为:"+rect.girth());                System.out.println("长方形的面积为:"+rect.area());            }        }

Day06 Java Basics Learning Notes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.