尚硅谷_Java零基础教程(面向对象中)--学习笔记(十一)

项目二

package com.atguigu.p2.bean;
/**
 * @Description Customer实体对象类,用来封装客户信息
 * @author yangwei
 *
 */
public class Customer {
	private String name;
	private char gender;
	private int age;
	private String phone;
	private String email;
	
	public Customer() {
		
	}
	
	public Customer(String name, char gender, int age, String phone, String email) {
		this.name = name;
		this.gender = gender;
		this.age = age;
		this.phone = phone;
		this.email = email;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public char getGender() {
		return gender;
	}
	public void setGender(char gender) {
		this.gender = gender;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
}
package com.atguigu.p2.service;

import com.atguigu.p2.bean.Customer;

public class CustomerList {
	private Customer[] customers;
	private int total = 0;
	
	/**
	 * @Description 用来初始化customer数组的构造器
	 * @param totalCustomer,指定数组的长度
	 */
	public CustomerList(int totalCustomer) {
		this.customers = new Customer[totalCustomer];
	}
	/**
	 * @Description 将指定的客户添加到数组中
	 * @param customer
	 * @return true:添加成功,false:添加失败
	 */
	public boolean addCustomer(Customer customer) {
		if(this.total >= this.customers.length) {
			return false;
		}
		
		customers[total] = customer;
		total++;
		
		return true;
	}
	/**
	 * @Description 修改指定索引位置的客户信息
	 * @param index
	 * @param cust
	 * @return true:修改成功,false:修改失败
	 */
	public boolean replaceCustomer(int index, Customer cust) {
		if(index < 0 || index >= total) {
			return false;
		}
		
		this.customers[index] = cust;
		
		return true;
	}
	/**
	 * @Description 删除指定索引上的客户
	 * @param index
	 * @return true:删除成功, false:删除失败
	 */
	public boolean deleteCustomer(int index) {
		if(index<0 || index >= this.total) {
			return false;
		}
		for(int i=index; i<this.total - 1;i++) {
			this.customers[i] = this.customers[i+1];
		}
		// 最好有数据的元素需要置空
		this.customers[total - 1] = null;
		total--;
		return true;
	}
	/**
	 * @Description 获取索引的客户信息
	 * @return
	 */
	public Customer[] getAllCustomers() {
		Customer[] custs = new Customer[total];
		for(int i=0; i<total;i++) {
			custs[i] = this.customers[i];
		}
		return custs;
	}
	
	/**
	 * @Description 获取指定索引位置上的客户
	 * @param index
	 * @return 如果找到了元素,则返回;如果没有找到,则返回null;
	 */
	public Customer getCustomer(int index) {
		if(index<0 || index>=total) {
			return null;
		}
		
		return this.customers[index];
	}
	/**
	 * @Description 获取存储客户的数量
	 * @return
	 */
	public int getTotal() {
		return this.total;
	}
}
package com.atguigu.p2.ui;

import com.atguigu.p2.bean.Customer;
import com.atguigu.p2.service.CustomerList;
import com.atguigu.p2.util.CMUtility;

public class CustomerView {
	private CustomerList customerList = new CustomerList(10);
	
	public CustomerView() {
		Customer customer = new Customer("1", '男', 23, "132", "123");
		this.customerList.addCustomer(customer);
	}
	/**
	 * @Description 显示界面的方法
	 */
	public void enterMainMenu() {
		
		boolean isFlag = true;
		while(isFlag) {
			System.out.println("\n客户信息管理软件");
			System.out.println("1.添加客户");
			System.out.println("2.修改客户");
			System.out.println("3.删除客户");
			System.out.println("4.客户列表");
			System.out.println("5.退出\n");
			System.out.println("请选择(1-5):");
			
			char menu = CMUtility.readMenuSelection();
			
			switch(menu) {
			case '1':
				addNewCustomer();
				break;
			case '2':
				modifyCustomer();
				break;
			case '3':
				deleteCustomer();
				break;
			case '4':
				listAllCustomers();
				break;
			case '5':
//				System.out.println("退出");
				System.out.print("确认是否退出:");
				char isExit = CMUtility.readConfirmSelection();
				if(isExit == 'Y') {
					isFlag = false;
				}
			}
//			isFlag = false;
		}
		
	}
	
	/**
	 * @Description 添加客户操作
	 */
	private void addNewCustomer() {
//		System.out.println("添加客户操作");
		System.out.println("添加客户");
		System.out.print("姓名:");
		String name = CMUtility.readString(10);
		
		System.out.print("性别:");
		char gender = CMUtility.readChar();
		
		System.out.print("年龄:");
		int age = CMUtility.readInt();
		
		System.out.print("电话:");
		String phone = CMUtility.readString(13);
		
		System.out.print("邮箱:");
		String email = CMUtility.readString(30);
		
		// 将上述数据封装到对象中
		Customer customer = new Customer(name, gender, age, phone, email);
		
		boolean isSuccess = this.customerList.addCustomer(customer);
		
		if(isSuccess) {
			System.out.println("添加完成");
		}else {
			System.out.println("添加失败");
		}
	}
	
	/**
	 * @Description 修改客户操作
	 */
	private void modifyCustomer() {
//		System.out.println("修改客户操作");
		System.out.println("修改客户");
		
		Customer cust;
		int number;
		for(;;) {
			System.out.print("请选择待修改客户的编号(-1退出):");
			number = CMUtility.readInt();
			
			if(number == -1) {
				return;
			}
			
			cust = customerList.getCustomer(number - 1);
			
			if(cust == null) {
				System.out.println("无法找到指定客户!");
			}else {//找到相应编号的客户
				break;
			}
		}
		//修改客户信息
		System.out.print("姓名(" + cust.getName() + "):");
		String name = CMUtility.readString(10, cust.getName());
		
		System.out.print("性别(" + cust.getGender() +"):");
		char gender = CMUtility.readChar(cust.getGender());
		
		System.out.print("年龄(" + cust.getAge() + "):");
		int age = CMUtility.readInt(cust.getAge());
		
		System.out.print("电话(" + cust.getPhone() + "):");
		String phone = CMUtility.readString(13, cust.getPhone());
		
		System.out.print("邮箱(" + cust.getEmail() + "):");
		String email = CMUtility.readString(30, cust.getEmail());
	
		Customer newCust = new Customer(name, gender, age, phone, email);
		
		boolean isReplaced = this.customerList.replaceCustomer(number-1, newCust);
		if(isReplaced) {
			System.out.println("修改完成");
		}else {
			System.out.println("修改失败");
		}
	}
	/**
	 * @Description 删除客户操作
	 */
	private void deleteCustomer() {
//		System.out.println("删除客户操作");
		System.out.println("删除客户");
		int number;
		
		for(;;) {
			System.out.print("倾选择待删除客户编号(-1退出):");
			number = CMUtility.readInt();
			
			if(number == -1) {
				return;
			}
			
			Customer cust = this.customerList.getCustomer(number - 1);
			if(cust == null) {
				System.out.println("无法找到指定客户");
			}else {
				break;
			}
		}
		
		//找到了指定客户
		System.out.println("是否要确认删除(Y/N)");
		char isDelete = CMUtility.readConfirmSelection();
		if(isDelete == 'Y') {
			this.customerList.deleteCustomer(number - 1);
			System.out.println("删除完成");
		}
	}
	
	/**
	 * @Description 显示客户列表操作
	 */
	private void listAllCustomers() {
		System.out.println("客户列表");
		
		int total = this.customerList.getTotal();
		if(total == 0) {
			System.out.println("没有客户记录");
		}else {
			System.out.println("编号\t姓名\t性别\t年龄\t电话\t邮箱");
			Customer[] custs = this.customerList.getAllCustomers();
			
			for(int i=0; i<custs.length; i++) {
				Customer cust = custs[i];
				System.out.println((i + 1) + "\t" + cust.getName() + "\t" + cust.getGender()+"\t" + cust.getAge() + "\t"+ cust.getPhone() + "\t" +cust.getEmail());
			}
		}
		System.out.println("客户列表完成");
	}
	
	public static void main(String[] args) {
		CustomerView view = new CustomerView();
		view.enterMainMenu();
	}
}

继承

package com.atguigu.java;

public class Person {
	String name;
	int age;
	
	public Person() {
		
	}
	
	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}
	
	public void eat() {
		System.out.println("吃饭");
	}
	
	public void sleep() {
		System.out.println("睡觉");
	}
}
package com.atguigu.java;

public class Student extends Person{
//	String name;
//	int age;
	String major;
	
	public Student() {
		
	}
	
	public Student(String name, int age, String major) {
		this.name = name;
		this.age = age;
		this.major = major;
	}
	
//	public void eat() {
//		System.out.println("吃饭");
//	}
	
//	public void sleep() {
//		System.out.println("睡觉");
//	}
	
	public void study() {
		System.out.println("学习");
	}
}
package com.atguigu.java;
/*
 * 面向对象的特征之二:继承性 why?
 * 一、继承性的好处
 * 1、减少了代码的冗余,提高了代码的复用性。
 * 2、便于功能的扩展。
 * 3、为之后的多态性的使用提供了前提。
 * 
 * 二、继承性的格式 class A extends B{}
 * 1、A:子类、派生类、subclass
 * 2、B:父类、超类、基类、superclass
 * 3、体现:一旦子类A继承了父类B以后,子类A中就获取了父类B中声明的结构,属性、方法。
 *		特别的,父类中声明为private的属性和方法,子类继承父类以后,仍然认为获取了父类中私有的结构。只是因为封装性的影响,使得子类不能直接调用父类的结构。
 * 4、子类继承父类以后,还可以声明自己特有的属性或方法,实现功能的扩展。
 * 5、子类和父类的关系,不同于集合和子集的关系。
 * 6、extends:扩展。
 * 
 * 三、Java中关于继承性的规定
 * 1、一个类可以被多个子类继承;
 * 2、一个类只能有一个父类:java中类的单继承;
 * 3、子父类是相对的概念。
 * 4、子类直接继承的父类,称为直接父类。
 * 5、子类继承父类以后,就获取类直接父类以及所有间接父类声明的属性和方法。
 *
 * 四、
 * 1、如果我们没有显式的声明一个类的父类的话,则此类继承与java.lang.Object类。
 * 2、所有的类(除	java.lang.Object)都直接或间接继承于java.lang.Object;
 * 3、意味着,所有的java类都具有java.lang.Object类声明的功能。
 */
public class ExtendsTest {
	public static void main(String[] args) {
		Person p1 = new Person();
		p1.age = 1;
		p1.eat();
		
		Student s1 = new Student();
		s1.eat();
		s1.sleep();
		s1.name = "Tom";
	}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值