综合练习2:购买电影票(代码区别:对象.成员方法)

综合练习2:购买电影票

购买电影票有优惠:满18周岁的付40元,未满18周岁的享受半价。使用成员变量、成员方法、构造方法和this关键字,控制台输出如下所示的姓名、年龄、票价等信息。

/* 综合练习2:购买电影票
 * 购买电影票有优惠:满18周岁的付40元,未满18周岁的享受半价。
 * 使用成员变量、成员方法、构造方法和this关键字,控制台输出如下所示的
 * 姓名、年龄、票价等信息。
 *  */

package Lhdx;

public class Zhlx2{
	String name;//成员变量
	int age;
	
	public Zhlx2(String name ,int age) {//构造方法、this关键字
		this.name = name;
		this.age = age;
	}
	
	public void buytickets() {//成员方法,无参数,直接使用成员变量
		if(age < 18) {
			System.out.println(name+"\t\t"+age+"\t\t"+40/2);
		}else {
			System.out.println(name+"\t\t"+age+"\t\t"+40);
		}
	}
	
	public static void main(String[] args) {
		System.out.println("姓名"+"\t\t"+"年龄"+"\t\t"+"票价(元)");
		System.out.println("————————————————————————————————————————————————————");
		Zhlx2 audience1 = new Zhlx2("李明", 20);
		audience1.buytickets();//对象直接调用成员方法
		Zhlx2 audience2 = new Zhlx2("钱丽", 16);
		audience2.buytickets();//对象直接调用成员方法
		Zhlx2 audience3 = new Zhlx2("周刚", 8);
		audience3.buytickets();//对象直接调用成员方法
		Zhlx2 audience4 = new Zhlx2("吴红", 32);
		audience4.buytickets();//对象直接调用成员方法
		
	}
	
	
}

//public class Zhlx2 {
//	
//	String name;
//	int age;
//	static int price;
//	
//	public Zhlx2(String name,int age) {
//		this.name = name;
//		this.age = age;
//	}
//	
//	public static int piaoprice(int age,int price) {
//		if(age < 18) {
//			price = 20;
//		}else {
//			price = 40;
//		}
//		return price;		
//	}
//
//	public static void main(String[] args) {
//		// TODO 自动生成的方法存根
//		
//		System.out.println("姓名\t年龄\t票价(元)");
//		System.out.println("————————————————————————————————————");
//		Zhlx2 cust1 = new Zhlx2("李明",20);
//		System.out.println(cust1.name+"\t"+cust1.age+"\t"+piaoprice(cust1.age,price));
//		
//		Zhlx2 cust2 = new Zhlx2("钱丽",16);
//		System.out.println(cust2.name+"\t"+cust2.age+"\t"+piaoprice(cust2.age,price));
//
//		Zhlx2 cust3 = new Zhlx2("周刚",8);
//		System.out.println(cust3.name+"\t"+cust3.age+"\t"+piaoprice(cust3.age,price));
//
//		Zhlx2 cust4 = new Zhlx2("吴红",32);
//		System.out.println(cust4.name+"\t"+cust4.age+"\t"+piaoprice(cust4.age,price));
//
//		
//
//	}
//
//}

(对象调用无参成员方法输入内容!成员方法操作成员变量)
在这里插入图片描述

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值