大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
案例:定义“手机”类
package top.djzxdx.objectoriented;
public class Phone {String brand;
int price;
public void call(){System.out.println("打电话");
}
public void sendMessage(){System.out.println("发短信");
}
}
对象的创建和使用需求:创建一个手机类的对象,品牌是小米,价格是1999元,并且使用手机类中的打电话和发短信方法。
package top.djzxdx.objectoriented;
public class PhoneDemo {public static void main(String[] args) {Phone p = new Phone();
p.brand = "小米";
p.price = 1999;
System.out.println(p.brand + " " + p.price + "元");
p.call();
p.sendMessage();
}
}
成员变量和局部变量成员变量和局部变量的区别private关键字this关键字案例:标准类制作
标准类:
package top.djzxdx.objectoriented2;
public class Student {private String name;
private int age;
public Student() {}
public Student(String name, int age) {this.name = name;
this.age = age;
}
public void setName(String name){this.name = name;
}
public String getName(){return name;
}
public void setAge(int age){this.age = age;
}
public int getAge(){return age;
}
public void show(){System.out.println(name + "..." + age);
}
}
测试类:
package top.djzxdx.objectoriented2;
public class StudentDemo {public static void main(String[] args) {Student s1 = new Student();
s1.setName("张三");
s1.setAge(28);
s1.show();
System.out.println("------------");
Student s2 = new Student();
s2.setName("李四");
s2.setAge(30);
System.out.println(s1.getName() +"," + s1.getAge());
System.out.println("------------");
Student s3 = new Student("赵5", 40);
s3.show();
}
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧