类的定义及其调用

在这里插入图片描述
phoneuse为主函数类
phone为应用类
应该放在同一个learnone包下下

每一个类前加上前缀

package learnone;

phone应用类

public class phone {
    String brand;
    String type;
    float price;
    
    public void call() {
        System.out.println("拨打电话");
    }

    public void message() {
        System.out.println("发送短信");
    }

    public void photo() {
        System.out.println("拍照");
    }
}

phonehouse主函数类

public class phoneuse {
    public static void main(String[] args){
        //格式:类名称 对象名 = new 类名称();
        phone ph = new phone();
        System.out.println(ph.brand);
        ph.call();
        ph.price = 2899;
        System.out.println(ph.price);
    }
}

如果没有

public static void main(String[] args){

的类是没办法运行的,只能调用

猜你喜欢

转载自blog.csdn.net/weixin_49321034/article/details/107429586
今日推荐