面向对象

一、对象:用来描述客观事物的一个实体,由一组属性和方法构成。

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中的属性,方法。


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值