一、对象:用来描述客观事物的一个实体,由一组属性和方法构成。
1.创建一个对象要在类里面。关系:对象是类的实例,类是对象的模板。对象通过new来调用类的方法。
2.先声明类的类型,再定义对象:Dome1.
Student age = new Student;//Student声明了类的类型;age则是Student类型的对象。
二、面向对象:
优点:①与人类的思维习惯一致;②提高了程序的可重用性;③提高了程序的可维护性和安全性。
Dome1。实现积分回馈功能;
金卡
客户积分大于
1000
分或普卡客户积分大于
5000
,获得回馈积分
500
分;
创建客户对象输出他得到的回馈积分;
package day01;
public class Customer {
int score;
String type;
public void show(){
if("金卡".equals(type)){
if(score>1000){
System.out.println("积分:"+score+"\t"+"卡类型:"+type+"\n"+"回馈积分:500分");
}else{
System.out.println("积分:"+score+"\t"+"卡类型:"+type+"\n"+"回馈积分:0分");
}
}else if("普卡".equals(type)){
if(score>5000){
System.out.println("积分:"+score+"\t"+"卡类型:"+type+"\n"+"回馈积分:500分");
}else{
System.out.println("积分:"+score+"\t"+"卡类型:"+type+"\n"+"回馈积分:0分");
}
}
}
}
注释:以上部分是创建Customer类,也是开头说到的模板。
package day01;
public class TestCus {
public static void main(String[] args) {
Customer customer=new Customer();
customer.type="金卡";
customer.score=600;
customer.show();
Customer customer2=new Customer();
customer2.score=1222;
customer2.type="金卡";
customer2.show();
}
}
注释:customer的实例,它可以调用Customer中的属性,方法。