Self day06_ the Java classes and objects, packaging, construction method

Object-oriented thinking

Object-oriented thinking Overview

Java is an object-oriented programming language, object-oriented programming is an idea, under the guidance of our object-oriented thinking, the use of the Java language to design, develop computer programs. The object here refers to the reality of all things, every thing all have their own attributes and behaviors. Object-oriented computer programming is in process, referring to the reality of things, the properties of the characteristics of things, abstracted behavioral characteristics, describe the design ideas into computer events. It is different from the process-oriented thinking, the emphasis is achieved by calling the object's behavior, rather than trying to operate step by step to achieve.

The difference between thinking

  • Process for: when the need to implement a function of each particular step must be hands, a detailed every detail.
  • Object-oriented: When the need to implement a function, do not care about the specific steps, but this function has been to find a person to help me do a thing.

Feature

  • A more object-oriented thinking is in line with our thinking habits of thought, it can be complex things simple, and will we become a commander from the performer. Object-oriented languages, comprising the three basic characteristics, i.e. encapsulation, inheritance and polymorphism.

Classes and Objects

What is the class

Class: is a group of related properties and behavior of the collection. It can be seen as a kind of template of things, use things attribute characteristics and behavioral characteristics to describe this kind of things.

In reality, we describe a class of things:

  • Properties: is the status of that thing.
  • Behavior: that is what this thing can do.
Example: kittens.
  • Attributes: name, weight, age, color.
  • Behavior: walking, running, called .

What is the object

Object: is a concrete manifestation of a class of things. Object is an instance of a class, it must have the properties and behavior of such things.

In reality, an instance of a class of things: a kitten.

For example: a cat.
  • Properties: tom, 5kg, 2 years, yellow.
  • Behavior: slip foot of the wall walk, jumping tap running, meow.

The relationship between classes and objects

  • Class is a description of a class of things, is abstract .
  • Objects are instances of a class of things, it is specific .
  • Class is a template object, the object is a class of entities.

Class definition

Compare things with class

The real world of a class of things:
  • Properties: status of things.
  • Behavior: things can do.
Java class used to describe things is true:
  • Member variables: the corresponding attribute of things
  • Member method: the corresponding behavior of things

Class Definition Format

Format Analysis:

The definition of class: is the definition of the class members, including member variables and member methods.
  • Member variable: in a class, the method of variable outer
  • Member method: and the previously defined method is almost the same. Just get rid of the static.

Code demonstrates how to define a class

package demo01;


/*
定义一个类,用来模拟“学生”事物。其中就有两个组成部分:

属性(是什么):
    姓名
    年龄
行为(能做什么):

对应到Java的类当中:
成员变量(属性):
    姓名
   年龄
成员方法(行为):
    吃饭
    睡觉
    学习

注意事项:
1. 成员变量是直接定义在类当中的,在方法外边。
2. 成员方法不要写static关键字。
 */
public class Student {

    // 成员变量
    String name; // 姓名
    int age; // 姓名

    // 成员方法
    public void eat() {
        System.out.println("吃饭饭!");
    }

    // 成员方法
    public void sleep() {
        System.out.println("睡觉觉!");
    }

    // 成员方法
    public void study() {
        System.out.println("学习!");
    }

}

对象的使用

通常情况下,一个类并不能直接使用,需要根据类创建一个对象,才能使用。

步骤如下:

1. 导包:也就是指出需要使用的类,在什么位置。

格式:import 包名称.类名称;

  • 对于和当前类属于同一个包的情况,可以省略导包语句不写。
  • java.lang包下的所有类无需导入

2. 创建,格式:

  • 类名称 对象名 = new 类名称();

3. 使用,分为两种情况:

  • 使用成员变量:对象名.成员变量名
  • 使用成员方法:对象名.成员方法名(参数)

定义测试类,使用我们刚才定义的Student类

package demo01;

public class Demo02Student {
    //我需要使用的Student类,和我自己位于同一个包下,所以省略导包语句不写
    public static void main(String[] args) {
        // 创建对象,格式:类名称 对象名 = new 类名称();
        Student stu = new Student();
        // 3. 使用其中的成员变量,格式:对象名.成员变量名
        System.out.println(stu.name); // null
        System.out.println(stu.age); // 0
        System.out.println("=============");

        // 将右侧的字符串,赋值交给stu对象当中的name成员变量
        stu.name = "赵丽颖";
        stu.age = 18;
        // 使用其中的成员变量,格式:对象名.成员变量名
        System.out.println(stu.name); // 赵丽颖
        System.out.println(stu.age); // 18
        System.out.println("=============");

        // 使用对象的成员方法,格式:对象名.成员方法名()
        stu.eat();
        stu.sleep();
        stu.study();
    }

}

代码执行后的结果

成员变量的默认值

如果成员变量没有进行赋值,那么将会有一个默认值,如下图所示。

类与对象的练习

定义手机类

package demo02;


/*
定义一个类,用来模拟“手机”事物。
属性:品牌、价格、颜色
行为:打电话、发短信

对应到类当中:
成员变量(属性):
    String brand; // 品牌
    double price; // 价格
    String color; // 颜色
成员方法(行为):
    public void call(String who) {} // 打电话
    public void sendMessage() {} // 群发短信
 */
public class Phone {

    // 成员变量
    String brand; // 品牌
    double price; // 价格
    String color; // 颜色

    // 成员方法
    public void call(String who) {
        System.out.println("给" + who + "打电话");
    }

    public void sendMessage() {
        System.out.println("群发短信");
    }
}

定义测试类

package demo02;


public class Demo01PhoneOne {

    public static void main(String[] args) {
        // 根据Phone类,创建一个名为one的对象
        // 格式:类名称 对象名 = new 类名称();
        Phone one = new Phone();
        System.out.println(one.brand); // null
        System.out.println(one.price); // 0.0
        System.out.println(one.color); // null
        System.out.println("=========");

        one.brand = "苹果";
        one.price = 8388.0;
        one.color = "黑色";
        System.out.println(one.brand); // 苹果
        System.out.println(one.price); // 8388.0
        System.out.println(one.color); // 黑色
        System.out.println("=========");

        one.call("乔布斯"); // 给乔布斯打电话
        one.sendMessage(); // 群发短信
    }

}

对象内存图

一个对象,调用一个方法内存图

通过上图,我们可以理解,在栈内存中运行的方法,遵循"先进后出,后进先出"的原则。变量p指向堆内存中的空间,寻找方法信息,去执行该方法。但是,这里依然有问题存在。创建多个对象时,如果每个对象内部都保存一份方法信息,这就非常浪费内存了,因为所有对象的方法信息都是一样的。那么如何解决这个问题呢?请看如下图解。

两个对象,调用同一方法内存图

对象调用方法时,根据对象中方法标记(地址值),去类中寻找方法信息。这样哪怕是多个对象,方法信息只保存一份,节约内存空间。

一个引用,作为参数传递到方法中内存图

 结论:引用类型作为参数,传递的是地址值。

成员变量和局部变量区别

变量根据定义位置的不同,我们给变量起了不同的名字。如下图所示:
在类中的位置不同
  • 成员变量:类中,方法外
  • 局部变量:方法中或者方法声明上(形式参数)
作用范围不一样 
  • 成员变量:类中
  • 局部变量:方法中
初始化值的不同 
  • 成员变量:有默认值
  • 局部变量:没有默认值。必须先定义,赋值,最后使用
在内存中的位置不同 
  • 成员变量:堆内存
  • 局部变量:栈内存
生命周期不同 
  • 成员变量:随着对象的创建而存在,随着对象的消失而消失
  • 局部变量:随着方法的调用而存在,随着方法的调用完毕而消失

封装

封装概述

面向对象编程语言是对客观世界的模拟,客观世界里成员变量都是隐藏在对象内部的,外界无法直接操作和修改。封装可以被认为是一个保护屏障,防止该类的代码和数据被其他类随意访问。要访问该类的数据,必须通过指定的方式。适当的封装可以让代码更容易理解与维护,也加强了代码的安全性。

原则

将属性隐藏起来,若需要访问某个属性,提供公共方法对其访问。

封装性在Java当中的体现:

  • 方法就是一种封装
  • 关键字private也是一种封装

封装的步骤

  1. 使用 private 关键字来修饰成员变量。
  2. 对需要访问的成员变量,提供对应的一对 getXxx 方法 、 setXxx 方法。

封装的操作

private的含义

  • private是一个权限修饰符,代表最小权限。
  • 可以修饰成员变量和成员方法。
  • 被private修饰后的成员变量和成员方法,只在本类中才能访问

private的使用

1:使用 private 修饰成员变量,代码如下:
package demo03;

public class Test {
    //private关键字修饰词成员变量
    private String name;
    private int age;
}
2. 提供 getXxx 方法 / setXxx 方法,可以访问成员变量,代码如下:
package demo03;

public class Test {
    //private关键字修饰词成员变量
    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;
    }


}

封装优化

this关键字

this的含义:this代表所在类的当前对象的引用(地址值),即对象自己的引用。记住 :方法被哪个对象调用,方法中的this就代表那个对象。即谁在调用,this就代表谁。

代码演示

package demo04;


/*
当方法的局部变量和类的成员变量重名的时候,根据“就近原则”,优先使用局部变量。
如果需要访问本类当中的成员变量,需要使用格式:
this.成员变量名

“通过谁调用的方法,谁就是this。”
 */
public class Person {

    String name; // 我自己的名字

    // 参数name是对方的名字
    // 成员变量name是自己的名字
    public void sayHello(String name) {
        System.out.println(name + ",你好。我是" + this.name);
    }

}

构造方法

作用:当一个对象被创建时候,构造方法用来初始化该对象,给对象的成员变量赋初始值。

构造方法的定义格式:

注意事项:

  • 构造方法的名称必须和所在的类名称完全一样,就连大小写也要一样
  • 构造方法不要写返回值类型,连void都不写
  • 构造方法不能return一个具体的返回值
  • 如果没有编写任何构造方法,那么编译器将会默认赠送一个构造方法,没有参数、方法体什么事情都不做。
  • 一旦编写了至少一个构造方法,那么编译器将不再赠送。
  • 构造方法也是可以进行重载的。重载:方法名称相同,参数列表不同

代码演示

package demo04;


/*
构造方法是专门用来创建对象的方法,当我们通过关键字new来创建对象时,其实就是在调用构造方法。
 */
public class Student {

    // 成员变量
    private String name;
    private int age;

    // 无参数的构造方法
    public Student() {

    }

    // 全参数的构造方法
    public Student(String name, int age) {
   
        this.name = name;
        this.age = age;
    }

    // Getter Setter
    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }

}

定义测试类

package demo04;


public class Demo02Student {

    public static void main(String[] args) {
        Student stu1 = new Student(); // 无参构造

        Student stu2 = new Student("赵丽颖", 20); // 全参构造
        System.out.println("姓名:" + stu2.getName() + ",年龄:" + stu2.getAge());//姓名:赵丽颖,年龄:20
        // 如果需要改变对象当中的成员变量数据内容,仍然还需要使用setXxx方法
        stu2.setAge(21); // 改变年龄
        System.out.println("姓名:" + stu2.getName() + ",年龄:" + stu2.getAge());//姓名:赵丽颖,年龄:21

    }

}

标准代码——JavaBean

JavaBean 是 Java语言编写类的一种标准规范。符合 JavaBean 的类,要求类必须是具体的和公共的,并且具有无参数的构造方法,提供用来操作成员变量的 set 和 get 方法。
格式:

编写符合 JavaBean 规范的类,以学生类为例,标准代码如下: 

package demo05;


/*
一个标准的类通常要拥有下面四个组成部分:

1. 所有的成员变量都要使用private关键字修饰
2. 为每一个成员变量编写一对儿Getter/Setter方法
3. 编写一个无参数的构造方法
4. 编写一个全参数的构造方法

这样标准的类也叫做Java Bean
 */
public class Student {

    private String name; // 姓名
    private int age; // 年龄

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = 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;
    }
}

定义测试类

package demo05;

public class Demo01Student {

    public static void main(String[] args) {
        Student stu1 = new Student();
        stu1.setName("迪丽热巴");
        stu1.setAge(20);
        System.out.println("姓名:" + stu1.getName() + ",年龄:" + stu1.getAge());
        System.out.println("=================");

        Student stu2 = new Student("古力娜扎", 21);
        System.out.println("姓名:" + stu2.getName() + ",年龄:" + stu2.getAge());
        stu2.setAge(22);
        System.out.println("姓名:" + stu2.getName() + ",年龄:" + stu2.getAge());
    }

}

代码执行后的结果

 

 

 

 

Guess you like

Origin www.cnblogs.com/wurengen/p/12128157.html