栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

对象,封装

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

对象,封装

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

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

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

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

 

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

package cn.tedu.oop;

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
汽车启动啦~
汽车停止啦~

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/319201.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号