0基础学java day08(面向对象,学生类和汽车类的创建和测试)

//1)创建学生类

package ooday01;
//学生类
/*//若自己不写构造方法,则编辑器默认提供一个无参构造方法,若写了构造方法就没默认的了
* 构造方法可以重载
* */
public class Student {
    //成员变量
    String name;
    int age;
    String className;
    String stuId;
    String hobby;
    Student(){};
    //给成员变量赋初始值        //局部变量(当前方法中)
    Student(String name,int age){

    };

    Student(String name1,int age1,String className1,String stuId1) {
             //成员变量  //局部变量
             this.name=name1;
             this.age=age1;
             this.className=className1;
             this.stuId=stuId1;
    }


    //方法
    //this只能用在方法中,方法中访问成员变量之前默认有个this
    void study(){
        System.out.println(this.name+"在学习...");
    }
    void sayHi(){
        System.out.println("大家好,我叫"+this.name+",今年"+age+
        "岁了"+"所在班级"+this.className+"学号为:"+this.stuId);
    }

    void playWitch(String anotherName){
        System.out.println(this.name+"正在和"+anotherName+"一起玩...");
    }
    void eat(){
        System.out.println(this.hobby);
    };
}

//2) 测试学生类

package ooday01;
//学生的测试类
//引用类型变量(引用)  基本数据类型变量(变量)
//数据类型    引用    指向     对象
//Student  zs   =   new Student();
public class StudentTest {
    public static void main(String[] args) {
    //创建学生对象
        Student zs=new Student();//声明Student类型的引用zs指向一个学生对象
        Student ls=new Student();//声明Student类型的引用zs指向一个学生对象

        zs.name="张三";
        zs.age=24;
        zs.className="jsd2302";
        zs.stuId="23001";

        ls.name="李四";
        ls.age=24;
        ls.className="jsd2302";
        ls.stuId="23002";

    //调用方法
        zs.study();//study中的this指的是zs那个对象
        zs.sayHi();//sayHi中的this指的是zs那个对象
        zs.playWitch("lisi");
        zs.eat();//eat中的this指的是zs那个对象

        ls.study();//study中的this指的是ls那个对象
        ls.sayHi();//sayHi中的this指的是ls那个对象
        ls.playWitch("lisi");
        ls.eat();//eat中的this指的是ls那个对象


    }
}

//3)学生类构造方法的演示

package ooday01;
/**构造方法的演示*/
public class ConsDemo {
    public static void main(String[] args) {
        Student zs=new Student();
        Student ls=new Student("李四",25);
        Student ww=new Student("ww",18,"jsd2302","23003");
        // 访问成员变量

        zs.sayHi();
        ls.sayHi();
        ww.sayHi();

        zs.playWitch("ls");
        ls.playWitch("ww");
        ww.playWitch("zs");
    }
}

 //4)汽车类

package ooday01;

public class Car {
    
    String brand;
    String color;
    double price;

    Car(){};
    Car(String brand,String color,double price){
        this.brand=brand;
        this.color=color;
        this.price=price;
    };

    void start(){System.out.println(brand+"牌子的"+color+"的"+price+"的车启动了");};
    void run(){System.out.println(brand+"牌子的"+color+"的"+price+"的车开始跑了");};
    void stop(){System.out.println(brand+"牌子的"+color+"的"+price+"的车停止了");};
}



//5)测试汽车类 

package ooday01;

public class CarTest {
    public static void main(String[] args) {
        //有参的
        Car car=new Car("特斯拉","红色",222222.888);
        car.start();
        car.run();
        car.stop();
        //无参的
        car.brand="奥迪";
        car.color="白色";
        car.price=600000;
        car.start();
        car.run();
        car.stop();
    }
}

笔记:
1. 什么是类?什么是对象?
现实生活是由很多很多对象组成的,基于对象抽出了类
对象:软件中真实存在的单个的个体/东西
类:类型/类别,代表一类个体
类是对象的模子/模板,对象是类的具体的实例,可以将类理解为类别/模子/图纸
类中可以包含:
对象的属性/特征/数据----------------------成员变量
对象的行为/动作/功能----------------------方法
一个类可以创建多个对象
2. 如何创建类?如何创建对象?如何访问成员?
3. this:指代当前对象,哪个对象调用方法它指的就是哪个对象
只能用在方法中,方法中访问成员变量之前默认有个this.
this的用法:
this.成员变量名--------------------访问成员变量(必须掌握)
当成员变量与局部变量同名时,若想访问成员变量,则this不能省略
this.方法名()-------------------------调用方法(一般不用)
this()-----------------------------------调用构造方法(一般不用)
4. 构造方法:构造函数、构造器、构建器----------好处:复用给成员变量赋初始值的代码
作用:给成员变量赋初始值
语法:与类同名,没有返回值类型(连void都没有)
调用:在创建(new)对象时被自动调用
若自己不写构造方法,则编译器默认提供一个无参构造方法,若自己写了构造方法,则不再默
认提供
构造方法可以重载

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值