新手JAVA第二阶段_类和对象的详细介绍

第一章 类和对象

第一节 类和对象

1.类的由来

人们在日常生活当中经常会将具有相同特征或者相同行为的事物归为一类。在Java中,用来描述这类事物的就是Java类,Java类就是这样诞生的。Java是一门以类为组织单元的语言,我们定义的Java类就是一种Java数据类型,该数据类型属于引用数据类型。

2.如何定义类

语法

public class 类名{
    
}

示例

//人类
public class Person{
    
}

类定义好了,如何定义类中的内容?

找出人类共同的特征:姓名、性别、年龄

/**
 * 人类
 *
 */
public class Person {

    public String name; //姓名

    public String sex; //性别

    public int age; //年龄
}

找出人类共同的行为(或者叫做属性):吃饭、睡觉、工作

在类中如何描述人类的行为?

在java中使用方法来描述行为,方法的定义语法如下:

//“[]”中的内容表示可有可无
访问修饰符  返回值类型  方法名([参数列表]){
    [return 返回值;]
}

那么Person应该如下

/**
 * 人类
 *
 */
public class Person {

    public String name; //姓名

    public String sex; //性别

    public int age; //年龄

    public void eat(){
        System.out.println("人吃饭");
    }
    
    public void sleep(){
        System.out.println("人睡觉");
    }
    
    public void work(){
        System.out.println("人工作");
    }
}

定义一个计算器类,计算器能够对两个数字进行加减乘除。

分析

a.计算器能够接收两个数字和一个运算符

b.计算器能够进行计算

/**
 * 定义一个计算器类,计算器能够对两个数字进行加减乘除。
 * a.计算器能够接收两个数字和一个运算符
 * b.计算器能够进行计算
 *
 */
public class Calculator {
    
    public double number1;  //接受的数字1
    
    public double number2;  //接受的数字2
    
    public String operator; //接收的运算符
    
    public void calculate(){ //计算
        switch (operator){
            case "+":
                System.out.println(number1 + number2);
                break;
            case "-":
                System.out.println(number1 - number2);
                break;
            case "*":
                System.out.println(number1 * number2);
                break;
            case "/":
                System.out.println(number1 / number2);
                break;    
        }
    }
}

3.类图

类图用于描述类的结构,与流程图一样,简单直观,容易理解

public修饰的属性和方法前需要使用’+‘,private修饰的属性和方法前需要使用’-’

可以使用Processon网站绘制类图

在这里插入图片描述

4.类和对象的关系

解释说明

类是描述多个事物的共有特征和行为的一个抽象体。而对象是一个具体的事物,每一个属性和每一个行为都是具体的。类是对象的集合体。类是用来构建具体的对象的。

语法

类名 对象名 = new 类名();
对象名.属性名 = 属性值;

示例

public class PersonTest {

    public static void main(String[] args) {
        //这里p称为对象名,跟数组名一样,本质都是变量。只是在面向对象中称之为对象名
        //类名 对象名 = new 类名();
        Person p = new Person(); //构建了一个具体的人,只是这个人目前还没有名字,性别和年龄
        //对象名.属性名 = 属性值;
        p.name = "张德帅"; //给这个人开始赋属性
        p.sex = "男";
        p.age = 53;
    }
}

public class CalculatorTest {

    public static void main(String[] args) {
        Calculator c = new Calculator();//构建了一个具体的计算器
        c.number1 = 10;
        c.number2 = 5;
        c.operator = "*";
    }
}

Person类的引用图例

在这里插入图片描述

类既然是一类事物的共同特征和行为的描述,那么一个类应该可以描述多个事物,因此类也可以看见多个对象。

示例

public class PersonTest {

    public static void main(String[] args) {
        //这里p称为对象名,跟数组名一样,本质都是变量。只是在面向对象中称之为对象名
        Person p = new Person(); //构建了一个具体的人,只是这个人目前还没有名字,性别和年龄
        p.name = "张德帅";
        p.sex = "男";
        p.age = 53;
        
        Person p1 = new Person();
        p1.name = "张得丑";
        p1.sex = "男";
        p1.age = 52;
        
        Person p2 = new Person();
        p2.name = "匿名";
        p2.sex = "女";
        p2.age = 21;
    }
}

结论

类是对多个事物的抽象描述,描述的是它们的共同特征和行为举止。但需要注意的是:类中描述的共同特征,在对象创建出来之后是跟随对象走的。行为举止也是一样,属于对象。

练习

请使用类和对象的相关知识描述汽车类,并构造具体的对象

分析:

汽车的特征:品牌 型号 价格

汽车的行为:启动 加速 刹车

public class Car {

    public String brand; //品牌
    
    public String type; //型号
    
    public double price; //价格
    
    public void start(){
        System.out.println("汽车启动");
    }
    
    public void speedUp(){
        System.out.println("汽车加速");
    }
    
    public void stop(){
        System.out.println("汽车刹车");
    }
}
public class CarTest {

    public static void main(String[] args) {
        Car c = new Car();
        c.brand = "奥迪";
        c.type = "A8";
        c.price = 100000;
    }
}

第二节 成员变量和成员方法

1.成员变量

解释说明

在类中定义的变量就是成员变量。成员变量顾名思义是属于成员(具体的对象、具体的事物)的。成员变量有初始值。

成员变量的初始值:

引用数据类型的初始值都是null,整数类型的初始值都是0,浮点数类型是0.0,boolean类型是false,char类型是’\u0000’

访问成员变量的语法

对象名.属性名;

示例

public class PersonTest {

    public static void main(String[] args) {
        //这里p称为对象名,跟数组名一样,本质都是变量。只是在面向对象中称之为对象名
        Person p = new Person(); //构建了一个具体的人,只是这个人目前还没有名字,性别和年龄
        p.name = "张德帅";
        p.sex = "男";
        p.age = 53;
        System.out.println(p.name + "\t" + p.sex + "\t" + p.age);

        Person p1 = new Person();
        p1.name = "张得丑";
        p1.sex = "男";
        p1.age = 52;
        System.out.println(p1.name + "\t" + p1.sex + "\t" + p1.age);

        Person p2 = new Person();
        p2.name = "匿名";
        p2.sex = "女";
        p2.age = 21;
        System.out.println(p2.name + "\t" + p2.sex + "\t" + p2.age);
    }
}

练习

利用对象的属性展示汽车类的信息

public class CarTest {

    public static void main(String[] args) {
        Car c = new Car();
        c.brand = "奥迪";
        c.type = "A8";
        c.price = 100000;
        System.out.println(c.brand + "\t" + c.type + "\t" + c.price);
    }
}

2.成员方法

解释说明

在类中定义的方法就是成员方法。成员方法顾名思义是属于成员(具体的对象、具体的事物)的。

调用成员方法的语法

//[]中内容可有可无
对象名.方法名([参数列表]);

示例

/**
 * 人类
 *
 */
public class Person {

    public String name; //姓名

    public String sex; //性别

    public int age; //年龄

    public void eat(){
        System.out.println(age + "岁的" + sex + "性同志" + name + "吃饭");
    }

    public void sleep(){
        System.out.println(age + "岁的" + sex + "性同志" + name + "睡觉");
    }

    public void work(){
        System.out.println(age + "岁的" + sex + "性同志" + name + "工作");
    }
}
public class PersonTest {

    public static void main(String[] args) {
        //这里p称为对象名,跟数组名一样,本质都是变量。只是在面向对象中称之为对象名
        Person p = new Person(); //构建了一个具体的人,只是这个人目前还没有名字,性别和年龄
        p.name = "张德帅";
        p.sex = "男";
        p.age = 53;
        System.out.println(p.name + "\t" + p.sex + "\t" + p.age);
        p.eat();
        p.sleep();
        p.work();
    }
}

编译结果为:

在这里插入图片描述

练习

利用对象的方法展示汽车类的信息

/**
 * 汽车类
 * 汽车的特征:品牌 型号 价格
 * 汽车的行为:启动 加速 刹车
 *
 */
public class Car{

    public String brand;

    public String type;

    public double price;

    public void start(){
        System.out.println("汽车启动");
    }

    public void speedUp(){
        System.out.println("汽车加速");
    }
    public void stop(){
        System.out.println("汽车刹车");
    }
    public void show(){
        System.out.println(brand + "\t" + type + "\t" + price);
    }
}
public class CarTest {

    public static void main(String[] args) {
        Car c = new Car();
        c.brand = "奥迪";
        c.type = "A8";
        c.price = 100000;
        System.out.println(c.brand + "\t" + c.type + "\t" + c.price);
        c.show();
    }
}

编译结果为:

在这里插入图片描述

3.成员变量和局部变量

解释说明

在类中定义的变量就是成员变量。

在方法内部定义的变量就是局部变量。局部变量没有初始值。因此,局部变量在使用前必须完成初始化操作。当局部变量与成员变量同名时,局部变量的优先级更高

示例


public class Car{

    public String brand;

    public String type;

    public double price;

    public void show(){
    String brand = "奔驰"; //局部变量,没有初始值,因此使用前必须初始化
    System.out.println(brand + "\t" + type + "\t" + price);//这里brand用的是局部变量,因为局部变量的优先级更高,														    //并且范围就在所定义的方法内
	}
}

ublic class CarTest {

    public static void main(String[] args) {
        Car c = new Car();
        c.brand = "奥迪";
        c.type = "A8";
        c.price = 100000;
        System.out.println(c.brand + "\t" + c.type + "\t" + c.price);
        c.show();
    }
}

编译结果为:

在这里插入图片描述

4.this关键字

思考

在方法中,如果局部变量和成员变量同名,此时又想使用成员变量,怎么办呢?

此时需要使用this关键字来解决。

this关键字表示的是当前对象(使用new创建出来的对象)

示例


public class Car{

    public String brand;

    public String type;

    public double price;

    public void start(){
        System.out.println("汽车启动");
    }

    public void speedUp(){
        System.out.println("汽车加速");
    }
    public void stop(){
        System.out.println("汽车刹车");
    }
    public void show(){
        String brand = "奔驰"; //局部变量,没有初始值,因此使用前必须初始化
        System.out.println(this.brand + "\t" + type + "\t" + price);//这里brand用的是成员变量,因为this指向的														              //是成员,所以this.brand是的brand成员变量													  				   //而不是局部变量
    }
}
public class CarTest {

    public static void main(String[] args) {
        Car c = new Car(); // this => c
        c.brand = "奥迪";
        c.type = "A8";
        c.price = 100000;
        c.show();

        Car c1 = new Car(); // this => c1
        c1.brand = "大众";
        c1.type = "保时捷";
        c1.price = 150000;
        c1.show();
    }
}

编译结果为:

在这里插入图片描述

this还可以用来调用成员的方法

示例:

public class Person {

    public String name; //姓名

    public String sex; //性别

    public int age; //年龄

    public void eat(){
        System.out.println(age + "岁的" + sex + "性同志" + name + "吃饭");
        work();//相当于this.work();
    }

    public void sleep(){
        System.out.println(age + "岁的" + sex + "性同志" + name + "睡觉");
        work();//相当于this.work();
    }

    public void work(){
        System.out.println(age + "岁的" + sex + "性同志" + name + "工作");
    }
}

第三节 构造方法

1.概念

构造方法是一种特殊的方法,主要用于创建对象以及完成对象属性初始化操作。构造方法不能被对象调用。

2.语法

//[]中内容可有可无
访问修饰符 类名([参数列表]){
    
}

3.示例

/**
 * 汽车类
 *
 */
public class Car{

    public String brand;

    public String type;

    public double price;

    public Car(){ //对象属性初始化操作
        this.brand = "默认品牌";
        this.type = "默认型号";
        this.price = 5000;
    }

    public void show(){
        String brand = "奔驰"; //局部变量,没有初始值,因此使用前必须初始化
        System.out.println(this.brand + "\t" + type + "\t" + price);
    }
}
public class CarTest {

    public static void main(String[] args) {
        Car c = new Car(); // this => c
        c.show();//打印默认值
        c.brand = "奥迪";
        c.type = "A8";
        c.price = 100000;
        c.show();

        Car c1 = new Car(); // this => c1
        c1.brand = "大众";
        c1.type = "保时捷";
        c1.price = 150000;
        c1.show();
    }
}

编译结果为:

在这里插入图片描述

思考

在Car类中没有定义构造方法的时候,我们也可以这么使用在未赋值成员前c.show(),为什么?

/**
 * 汽车类
 * 汽车的特征:品牌 型号 价格
 * 汽车的行为:启动 加速 刹车
 *
 */
public class Car{

    public String brand;

    public String type;

    public double price;

    public Car(){ //构造方法就是用来创建对象以及完成对象的属性的初始化操作
       //这里没写,编译器会自动为成员赋上默认值
    }

    public void start(){
        System.out.println("汽车启动");
    }

    public void speedUp(){
        System.out.println("汽车加速");
    }
    public void stop(){
        System.out.println("汽车刹车");
    }
    public void show(){
        String brand = "奔驰"; //局部变量,没有初始值,因此使用前必须初始化
        System.out.println(this.brand + "\t" + type + "\t" + price);
    }
}

来自java官方的说明

类和对象

You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors. 

你不必为类提供任何构造方法,但是在执行此操作时一定要小心。编译器会自动为任何没有构造方法的类提供无参数的默认构造方法。

ystem.out.println(“汽车加速”);
}
public void stop(){
System.out.println(“汽车刹车”);
}
public void show(){
String brand = “奔驰”; //局部变量,没有初始值,因此使用前必须初始化
System.out.println(this.brand + “\t” + type + “\t” + price);
}
}


<font color = "red">**来自java官方的说明**</font>

[类和对象](https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html )

~~~tex
You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors. 

你不必为类提供任何构造方法,但是在执行此操作时一定要小心。编译器会自动为任何没有构造方法的类提供无参数的默认构造方法。
  • 34
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值