漫漫Java学习路第六天,OOP学习

package ce.tedu.oop;
//本类用作面向对象OOP入门案例
/*设计手机这一类事物
 * 分析属性:品牌 价格 尺寸 颜色
 * 分析功能:打电话 发短信 看直播*/
/*在一个java文件中可以写多个class,但是被public修饰的class只能有一个
* 并且要求这个公共类的名字必须与文件名一致*/
public class TestCar {
    public static void main(String[] args) {
        Car c1 = new Car();
        c1.brand = "宝马";
        c1.color = "五彩斑斓的黑";
        c1.speed = 100.1;
        c1.id = 666;
        c1.load = 4;
        System.out.println(c1.brand);
        System.out.println(c1.color);
        System.out.println(c1.id);
        System.out.println(c1.speed);
        System.out.println(c1.load);
        c1.start();
        c1.stop();
        Car c2 = new Car();
        c2.brand = "特斯拉";
        c2.color = "黑不溜秋的白";
        c2.speed = 120.5;
        c2.id = 34;
        c2.load = 2;
        System.out.println(c2.brand);
        System.out.println(c2.color);
        System.out.println(c2.id);
        System.out.println(c2.speed);
        System.out.println(c2.load);
        c2.start();
        c2.stop();
    }
}

//1.通过class关键字创建了一个汽车类
class Car{
    String brand;
    String color;
    double speed;
    int id;
    int load;

    public static void start(){
        System.out.println("不好意思,献丑了");
    }
    public static void stop(){
        System.out.println("轰!发动机爆缸了!!!");
    }
}
package ce.tedu.oop;
//本类用于面向对象的练习巩固
public class TestCar2 {
    public static void main(String[] args) {
        Car2 c2 = new Car2();
       /* c2.color = "暗夜紫";
        c2.brand = "BMW";
        c2.price = 200;
        c2.length = 0.2;
        System.out.println(c2.brand);
        System.out.println(c2.color);
        System.out.println(c2.price);
        System.out.println(c2.length);*/
        //c2.strat();
        //c2.stop();
        c2.setBrand("特斯拉");
        c2.setColor("布莱克黑");
        c2.setPrice(200.02);
        c2.setLength(3);
        System.out.println(c2.getBrand());
        System.out.println(c2.getColor());
        System.out.println(c2.getPrice());
        System.out.println(c2.getLength());
        c2.strat();
    }
}
class Car2{
    private String color;
    private String brand;
    private double price;
    private double length;
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public double getLength() {
        return length;
    }
    public void setLength(double length) {
        this.length = length;
    }
    public void strat(){
        System.out.println("油门踩进油箱里");
        stop();
    }
    private void stop(){
        System.out.println("刹车失灵了");
    }
}
package ce.tedu.oop;
//本类用作面向对象OOP入门案例
/*设计手机这一类事物
 * 分析属性:品牌 价格 尺寸 颜色
 * 分析功能:打电话 发短信 看直播*/
/*在一个java文件中可以写多个class,但是被public修饰的class只能有一个
* 并且要求这个公共类的名字必须与文件名一致*/
public class TestCreateClass {
    public static void main(String[] args) {
        //4.创建手机类的对象
        //使用new关键字创建对应类的对象
       Phone p1 = new Phone();
       //5.通过手机类的对象p1,调用手机类的功能
       p1.call();
       p1.message();
       p1.video();
       //6.通过手机类的对象p1,查看对象的属性值
       System.out.println(p1.brand);
       System.out.println(p1.color);
       System.out.println(p1.price);
       System.out.println(p1.size);
       //7.创建手机类的第二个对象
        Phone p2 = new Phone();
        //8.通过p2对象,调用手机类的3个方法
        p2.call();
        p2.message();
        p2.video();
        //9.给p2对象的4个属性赋值
        p2.brand = "HUAWEI";
        p2.color = "星空黑";
        p2.price = 8888.88;
        p2.size = 5.6;
        //10.打印p2对象的四个属性值
        System.out.println(p2.brand);
        System.out.println(p2.size);
        System.out.println(p2.price);
        System.out.println(p2.color);
        //11.两个对象的地址值并不相同
        System.out.println(p1);
        System.out.println(p2);
    }
}
//1.通过class关键字创建了一个手机类
class Phone{
    //2.定义手机类的属性---用成员变量来描述--位置:类里方法外
    String brand;
    double price;
    double size;
    String color;

    //3.定义手机类的功能--用方法来描述--修饰符 返回值类型 方法名(参数列表){方法体}
    public void call(){
        System.out.println("正在打电话");
    }
    public void message(){
        System.out.println("正在发短信");
    }
    public void video(){
        System.out.println("正在看直播");
    }
}
package ce.tedu.oop;
//本类用作面向对象OOP入门案例
/*设计手机这一类事物
 * 分析属性:品牌 价格 尺寸 颜色
 * 分析功能:打电话 发短信 看直播*/
/*在一个java文件中可以写多个class,但是被public修饰的class只能有一个
* 并且要求这个公共类的名字必须与文件名一致*/
public class TestCreateClass2 {
    public static void main(String[] args) {
        Phone2 p3 = new Phone2();
        p3.call();
        p3.message();
        p3.video();
        System.out.println(p3.brand);
        System.out.println(p3.color);
        System.out.println(p3.price);
        System.out.println(p3.size);

        Phone2 p4 = new Phone2();
        p4.call();
        p4.message();
        p4.video();
        p4.brand = "锤子";
        p4.color = "骚粉色";
        p4.price = 5466.66;
        p4.size = 6.5;
        System.out.println(p4.brand);
        System.out.println(p4.color);
        System.out.println(p4.size);
        System.out.println(p4.price);
    }
}

//1.通过class关键字创建了一个手机类
class Phone2{
    String brand;
    double price;
    double size;
    String color;
    public static void call(){
        System.out.println("正在打诈骗电话");
    }
    public static void message(){
        System.out.println("正在发骚扰短信");
    }
     public static void video(){
        System.out.println("看美女跳脱衣舞");
    }
}
package ce.tedu.oop;

public class TestDemo {
    public static void main(String[] ages){
        User2 u = new User2();
        u.setName("吕布");
        System.out.println(u.getName());
    }
}
class User2{
    private String name;
    private double price;
    private String color;
    private String eat;
    private int hour;
    private String access;
    public int getHour() {
        return hour;
    }
    public void setHour(int hour) {
        this.hour = hour;
    }
    public String getAccess() {
        return access;
    }
    public void setAccess(String access) {
        this.access = access;
    }

    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getEat() {
        return eat;
    }
    public void setEat(String eat) {
        this.eat = eat;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
}
package ce.tedu.oop;
//本类用于测试封装的必要性
public class TestPrivate1 {
    public static void main(String[] args) {
        User u = new User();
        //u.name = "李逵";
        //需要封装属性,如果不封装,就可以直接修改这个属性的值,不安全
        //u.money = 1000000000;
        //System.out.println(u.name);
        //System.out.println(u.money);
        u.setMoney(2000);
        System.out.println(u.getMoney());
        u.setName("李鬼");
        System.out.println(u.getName());
    }
}
class User{
    private String name = "李逵";
    //封装属性--通过private关键字修饰属性即可
    private double money = 1000;
    public double getMoney() {
        //后续可以在这个位置添加权限效验的代码
        return money;
    }
    public void setMoney(double money) {
        //后续可以在这个位置添加权限效验的代码
        //当本类的成员变量与局部变量重名时,可以使用this关键字指定本类的成员变量
        this.money = money;
    }
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name = name;
    }
}
package ce.tedu.oop;
//本类用于练习oop的第一大特性:封装
public class TestPrivate2 {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.setName("窃格瓦拉");
        s1.setSno(666);
        s1.setSubject("电瓶杀手");
        System.out.println(s1.getName());
        System.out.println(s1.getSno());
        System.out.println(s1.getSubject());
        Student s2 = new Student();
        s2.study();
        s2.eat();
        s2.slssp();
        s2.setName("曾小贤");
        s2.setSubject("徒手劈榴莲");
        s2.setSno(120);
        System.out.println(s2.getName());
        System.out.println(s2.getSno());
        System.out.println(s2.getSubject());
    }
}
class Student{
    private int sno;
    private String name;
    private String subject;
    public void study(){
        System.out.println("学习偷电瓶");
    }
    public void eat(){
        System.out.println("吃牢饭");
    }
    public void slssp(){
        System.out.println("在你睡觉的时候,你的电瓶就已经没了");
    }
    public int getSno() {
        return sno;
    }
    public void setSno(int sno) {
        this.sno = sno;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSubject() {
        return subject;
    }
    public void setSubject(String subject) {
        this.subject = subject;
    }
}
package ce.tedu.oop;
//本类用于练习方法的封装
public class TestPrivate3 {
    public static void main(String[] args) {
        Apple a1 = new Apple();
        a1.clean();
        //a1.eat();
    }
}
class Apple{
    public void clean(){
        System.out.println("苹果要先洗一洗");
        //当eat()方法被封装,外部其他的类是无法直接调用的
        //所以我们可以在本类Apple类中的clean()里直接调用eat()
        eat();
    }
    //private是一个修饰符,被private修饰的资源是私有资源,只能在本类中使用
    private void eat() {
        System.out.println("吃前先削皮");
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值