封装的定义及练习题

封装的定义

是将属性和方法组合成类同时隐藏类内部实现细节的机制。

类的封装遵循的原则

内聚性 / 一致性 /封装性 / 清晰性 / 完整性

在这里插入图片描述

访问修饰符的定义

是用于限定类型及类型成员可见性等级的修饰符(修饰符有三种,分别是private protected public )。

在这里插入图片描述
在这里插入图片描述

package test;

public class Net {
	private String web;//网虫套餐
	private String superman;//超人套餐
	private String talk;//话痨套餐
	
	//设置get和set方法
	public String getWeb() {
		return web;
	}
	public void setWeb(String web) {
		this.web = web;
	}
	public String getSuperman() {
		return superman;
	}
	public void setSuperman(String superman) {
		this.superman = superman;
	}
	public String getTalk() {
		return talk;
	}
	public void setTalk(String talk) {
		this.talk = talk;
	}
}

在这里插入图片描述

package test;

public class Stuedent {
	//成员变量(隐藏只需加private,设置为私有化,外界无法访问)
	private String id;//学号
	private String name;//姓名
	private int age;//年龄
	private String sex;//性别
	
	//如何设置值(学号),通过传参的方式赋值
	public void setId(String id){
		this.id = id;
	}
	//如何获取值,提供了一个访问值得通道
	public String getId(){
		return this.id;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}	
}
package test;

public class test_Student {

	public static void main(String[] args) {
		Stuedent zhangsan = new Stuedent();
		zhangsan.setId("10001");
		zhangsan.setAge(18);
		zhangsan.setSex("男");
		zhangsan.setName("张三");
	
		
		//对性别进行合法性判断
		if((zhangsan.getSex()== "女") || (zhangsan.getSex()== "男")){
			System.out.println("性别合法");
			System.out.println(zhangsan.getId());
			System.out.println(zhangsan.getAge());
			System.out.println(zhangsan.getSex());
			System.out.println(zhangsan.getName());
		}else{
			System.out.println("性别不合法。");
		}
	}
}

在这里插入图片描述

package test;

public class pxpress_person {//快递员类
	//成员变量定义,并封装
	private String number;//员工的编号
	private String name;//员工的姓名
	private int salary;//员工的薪资
	
	//无参构造方法
	public pxpress_person(){
		this.number = "0001";
		this.name = "刘强";
		this.salary = 1;
	}
	
	//有参构造
	/*无参构造器也叫无参构造方法,在我们创建类时候,可以不用写构造方法,
	 * 因为系统会默认给我们提供一个无参构造方法,如果我们自己写了无参构造方法,
	 * 那么我们这个就将默认的覆盖了。*/
	/**构造方法无返回值*/
	public pxpress_person(String number,String name,int salary){
		this.number = number;
		this.name = name;
		this.salary = salary; 
	}

	public void setNumber(String number){
		this.number = number;
	}
	public String getNumber(){
		return this.number;
	}
	
	public void setName(String name){
		this.name = name;
	}
	
	public String getName(){
		return this.name;
	}
	
	public void setSalary(int salary){
		this.salary = salary;
	}
	public int getSalary(){
		return this.salary;
	}
}
package test;

public class pxpress_person1 {
	public static void main(String[] args) {
		//pxpress_person LiuQiangdong = new pxpress_person();//无参构造
		pxpress_person LiuQiangdong = new pxpress_person("0001","刘强东",1);//有参构造
		
		System.out.println("员工编号:"+LiuQiangdong.getNumber());
		System.out.println("员工姓名:"+LiuQiangdong.getName());
		System.out.println("员工薪水:"+LiuQiangdong.getSalary());
	}
}

在这里插入图片描述

package test;

public class Cat {
	
	//定义三个变量(均为私有)
	private String name;//猫咪的姓名
	private int age;//猫咪的年龄
	private String color;//猫咪的颜色
	

	//为三个变量提供get和set方法
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	
	//构造有参函数
	public Cat(String name,String color,int age){
		this.name =name;
		this.age = age;
		this.color = color;
	}
}
package test;

import java.util.Scanner;

public class test_Cat {

	public static void main(String[] args) {
		//创建测试类及定义打印信息的方法
		System.out.print("请输入小猫的名字:");
		Scanner src = new Scanner(System.in);
		String input = src.next();
		Cat  xiaohua =new Cat("小花","白色",3);
		Cat  xiaobai =new Cat("小白","花色",3);
		
		if(input.equals(xiaohua.getName())){
			System.out.println("小猫的名字:"+xiaohua.getName());
			System.out.println("小猫的年龄:"+xiaohua.getAge());
			System.out.println("小猫的颜色:"+xiaohua.getColor());
		}else if(input.equals(xiaobai.getName())){
			System.out.println("小猫的名字:"+xiaobai.getName());
			System.out.println("小猫的年龄:"+xiaobai.getAge());
			System.out.println("小猫的颜色:"+xiaobai.getColor());
		}else{
			System.out.println("张老太没有这只猫。");
		}
	}
}

在这里插入图片描述
第五题表意不清,可以不做。
在这里插入图片描述

//网虫类
package Test;
 
public class WangChongTaoCan {
	private String name;
	private double price;
	private int no;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	public WangChongTaoCan(String name, double price, int no) {
		super();
		this.name = name;
		this.price = price;
		this.no = no;
	}
	public WangChongTaoCan() {
		super();
		// TODO Auto-generated constructor stub
	}
}
 
 
//超人类
package Test;
 
public class ChaoRenTaoCan {
	private String name;
	private double price;
	private int no;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	public ChaoRenTaoCan(String name, double price, int no) {
		super();
		this.name = name;
		this.price = price;
		this.no = no;
	}
	public ChaoRenTaoCan() {
		super();
		// TODO Auto-generated constructor stub
	}
	
}
 
//话痨类
package Test;
 
public class HuaLaoTaoCan {
	private String name;
	private double price;
	private int no;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	public HuaLaoTaoCan(String name, double price, int no) {
		super();
		this.name = name;
		this.price = price;
		this.no = no;
	}
	public HuaLaoTaoCan() {
		super();
		// TODO Auto-generated constructor stub
	}
	
}
 
//测试类
package Test;
 
import Test4.Cat;
 
public class Test6 {
	public static void main(String[] args) {
		WangChongTaoCan wc1=new WangChongTaoCan("网虫1",6.55,1);
		ChaoRenTaoCan cr1=new ChaoRenTaoCan("超人1",5.55,2);
		HuaLaoTaoCan hl1=new HuaLaoTaoCan("话痨1",8.55,3);
		
		WangChongTaoCan wc2=new WangChongTaoCan();
		wc2.setName("网虫2");
		wc2.setPrice(6.22);
		wc2.setNo(4);
		ChaoRenTaoCan cr2=new ChaoRenTaoCan();
		cr2.setName("超人2");
		cr2.setPrice(7.58);
		cr2.setNo(5);
		HuaLaoTaoCan hl2=new HuaLaoTaoCan();
		hl2.setName("话痨2");
		hl2.setPrice(3.25);
		hl2.setNo(6);
		
		System.out.println(wc1.getName());
		System.out.println(wc1.getPrice());
		System.out.println(wc1.getNo());
		
		System.out.println(wc2.getName());
		System.out.println(wc2.getPrice());
		System.out.println(wc2.getNo());
		
		System.out.println(cr1.getName());
		System.out.println(cr1.getPrice());
		System.out.println(cr1.getNo());
		
		System.out.println(cr2.getName());
		System.out.println(cr2.getPrice());
		System.out.println(cr2.getNo());
		
		System.out.println(hl1.getName());
		System.out.println(hl1.getPrice());
		System.out.println(hl1.getNo());
		
		System.out.println(hl2.getName());
		System.out.println(hl2.getPrice());
		System.out.println(hl2.getNo());
	}
}

对第一题进行扩展,要求如下。

:第一题进行扩展,套餐名称,价格,有效期,剩余流量;方法为输入套餐名称,展示套餐内容,输入要使用的流量,看套餐够不够,在输入使用天数,判断是否能够使用。

package test;

public class One {
/* 秦老师留的第一题进行扩展,
 * 套餐名称,价格,有效期,剩余流量;方法为输入套餐名称,
 * 展示套餐内容,输入要使用的流量,看套餐够不够,在输入使用天数,
 * 判断是否能够使用。*/
	
	private String name;//套餐名称
	private double price;//价格
	private int time;//有效期
	private double flow;//剩余流量
	
	//定义有参构造函数
	public One(String name,double price,int time,double flow){
		this.name = name;
		this.price = price;
		this.time = time;
		this.flow = flow;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public int getTime() {
		return time;
	}
	public void setTime(int time) {
		this.time = time;
	}
	public double getFlow() {
		return flow;
	}
	public void setFlow(double flow) {
		this.flow = flow;
	}
}

package test;

import java.util.Scanner;

public class test_One {

	public static void main(String[] args) {
		One wangchong = new One("网虫套餐",39.9,30,12.8);
		One chaoren = new One("超人套餐",29.8,50,10.9);
		One hualao = new One("话痨套餐",59.9,120,50);
		Scanner src = new Scanner(System.in);
		
		while(true){
			System.out.print("请输入你想选择的套餐名称(例:网虫套餐):");
			String input = src.next();
			System.out.println("时输入要使用的流量数 :");
			Double useflow = src.nextDouble();
			System.out.println("请输入使用天数:");
			int day = src.nextInt();
			
		if(input.equals(wangchong.getName())){
			if(useflow > wangchong.getFlow()){
				System.out.println("对不起,您使用的流量已将超出"+(wangchong.getFlow()-useflow)+"G");
				System.out.println("剩余天数为:"+(wangchong.getTime() - day));
			}else{
					System.out.println("套餐名称:"+wangchong.getName());
					System.out.println("套餐价格:"+wangchong.getPrice());
					System.out.println("套餐有效期限:"+wangchong.getTime());
					System.out.println("套餐剩余流量:"+wangchong.getFlow()+"G");
			}
			
		}else if(input.equals(chaoren.getName())){
			
			if(useflow > chaoren.getFlow()){
				System.out.println("对不起,您使用的流量已将超出"+(chaoren.getFlow()-useflow)+"G");
				System.out.println("剩余天数为:"+(chaoren.getTime() - day));
			}else{
				System.out.println("套餐名称:"+chaoren.getName());
				System.out.println("套餐价格:"+chaoren.getPrice());
				System.out.println("套餐有效期限:"+chaoren.getTime());
				System.out.println("套餐剩余流量:"+chaoren.getFlow()+"G");	
			}
			
		}else if(input.equals(hualao.getName())){
			if(useflow > hualao.getFlow()){
				System.out.println("对不起,您使用的流量已将超出"+(hualao.getFlow()-useflow)+"G");
				System.out.println("剩余天数为:"+(hualao.getTime() - day));
			}else{

				System.out.println("套餐名称:"+hualao.getName());
				System.out.println("套餐价格:"+hualao.getPrice());
				System.out.println("套餐有效期限:"+hualao.getTime());
				System.out.println("套餐剩余流量:"+hualao.getFlow()+"G");	
			}
			
		}else{
			System.out.println("请按照正确的格式输入套餐名称。");
		}
		
	  }
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值