对象,封装

面向过程:是一种编程思想,强调的是过程,凡事情亲历亲为

面向对象:是一种编程思想,强调的是过程。

类:是抽象的,类似于类型,我们可以理解成一类事物的模板。

对象:是具体的,是根据类具体创造出来的事物。

 

一个类可以创建多个对象,对象在内存中独立。

package cn.tedu.oop;
/**
 * 本类用于面向对象入门案例
 * 在一个Java文件中可以写多个class,但是被public修身的class只能有一个
 * 而且这个公共类的名字要求就是当前文件的名字
 */
class TestCreatClass {
    public static void main(String[] args) {
        phone p = new phone();
        p.call();
        p.message();
        p.video();
        System.out.println(p.brand);
        System.out.println(p.price);
        System.out.println(p.size);
        System.out.println(p.color);
        phone p2=new phone();
        System.out.println(p2);
        System.out.println(p2.brand="huawei");
        System.out.println(p2.price=15000);
        System.out.println(p2.size=16);
        System.out.println(p2.color="蓝色");
        p2.call();
        p2.message();
        p2.video();

    }
}
    class phone{
        String brand;//品牌
        double price;//价钱
        double size;//尺寸
        String color;//颜色
        public void call(){
            System.out.println("正在打电话");
        }
        public void message(){
            System.out.println("正在发短信");
        }
        public void video(){
            System.out.println("正在看直播");
        }
    }


正在打电话
正在发短信
正在看直播
null
0.0
0.0
null
cn.tedu.oop.phone@1b6d3586
huawei
15000.0
16.0
蓝色
正在打电话
正在发短信
正在看直播


 

调用方法的三种方法:2和3其实是一样

public class MethoDemo {
    public static void main(String[] args) {
        //方法1   我是f2()
        f2();
        //方法2  我是f2()
        //您好
        String a=f2();     方法2效率高一些
        System.out.println(a);
        //方法3  我是f2()  方法3经常调用,效率稍微低一些
        //您好 
        System.out.println(f2());
    }
    public static  String f2(){
        System.out.println("我是f2()");
        return "您好";
    }
}
我是f2()
我是f2()
您好
我是f2()
您好

封装思路:

1.用private修饰资源

2.提供

公共的get方法---用来获取值

提供巩固的set方法---用来设置值

封装方法的思路:

1.用private修饰资源

2.用本类的公共方法调用这个被封装方法的功能

调用start(),里面包括stop()方法。

package cn.tn;

public class TestCar {
    public static void main(String[] args) {
        Car c=new Car();
        c.setBrand("特斯拉");
        c.setColor("暗夜黑");
        c.setLength(19.0);
        c.setPrice(10903);
        System.out.println(c.getBrand()+" "+c.getColor()+" "+c.getLength()+"  "+c.getPrice());
        c.start();
    }
}
//1.抽象汽车这一类事物的共同点,用class描述
class Car{
    //2.属性--用成员变量来描述
    private String brand;//品牌
    private String color;//颜色
    private  double price;//价位
    private double length;//车长

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    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;
    }

    //3.功能--用方法来描述
    public void start(){
        System.out.println("汽车启动啦~");
        //在本类的方法里调用被封装的方法
        stop();
    }
    //封装汽车停止的方法。
    private void stop(){
        System.out.println("汽车停止啦~");
    }
}


特斯拉 暗夜黑 19.0  10903.0
汽车启动啦~
汽车停止啦~

猜你喜欢

转载自blog.csdn.net/weixin_43762083/article/details/120707395