每日一考和复习
每日一考
- 构造器的作用是什么?使用中有哪些注意点(>=3条)
// 作用
1.创建对象
2.属性初始化
// 注意点
1.Java语言中,每个类都至少有一个构造器
2.默认构造器的修饰符与所属类的修饰符一致
3.一旦显式定义了构造器,则系统不再提供默认构造器
4.一个类可以创建多个重载的构造器
5.父类的构造器不可被子类继承
- 关于类的属性的赋值,有几种赋值的方式。谈谈赋值的先后顺序
// 按以下先后顺序
1.默认初始化
2.显示初始化
3.构造器中初始化
4.通过“对象.属性”或“对象.方法”的方式赋值
顺序为1->2->3->4
- this关键字可以用来调用哪些结构,简单说明一下其使用。
方法 属性 构造器
方法和属性调用:this.function() this.value,本类没有会找父类
构造器:this(其它构造器的参数列表),必须声明在首行
- Java中目前学习涉及到的四种权限修饰符都有什么?并说明各自的权限范围
修饰符 | 类内部 | 同一个包 | 不同包的子类 | 同一个工程 |
---|---|---|---|---|
private | Yes | |||
缺省 | Yes | Yes | ||
protected | Yes | Yes | Yes | |
public | Yes | Yes | Yes | Yes |
- 创建Circle类,提供私有的radius属性,提供相应的get和set方法,提供求圆面积的方法。
public class Circle {
private double radius;
public void setRadius(double radius) {
this.radius = radius;
}
public double getRadius() {
return this.radius;
}
public double getArea() {
return Math.PI*(this.radius * this.radius);
}
}
复习
day10的学习内容
项目二:客户信息管理软件
以下为我自己实现的代码,也可以看参考答案
// 题目提供的CMUtility.java
package com.water.project02;
import java.util.*;
/**
* CMUtility工具类: 将不同的功能封装为方法,就是可以直接通过调用方法使用它的功能,而无需考虑具体的功能实现细节。
*/
public class CMUtility {
private static Scanner scanner = new Scanner(System.in);
/**
* 用于界面菜单的选择。该方法读取键盘,如果用户键入’1’-’5’中的任意字符,则方法返回。返回值为用户键入字符。
*/
public static char readMenuSelection() {
char c;
for (;;) {
String str = readKeyBoard(1, false);
c = str.charAt(0);
if (c != '1' && c != '2' && c != '3' && c != '4' && c != '5') {
System.out.print("选择错误,请重新输入:");
} else
break;
}
return c;
}
/**
* 从键盘读取一个字符,并将其作为方法的返回值。
*/
public static char readChar() {
String str = readKeyBoard(1, false);
return str.charAt(0);
}
/**
* 从键盘读取一个字符,并将其作为方法的返回值。 如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
*/
public static char readChar(char defaultValue) {
String str = readKeyBoard(1, true);
return (str.length() == 0) ? defaultValue : str.charAt(0);
}
/**
* 从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
*/
public static int readInt() {
int n;
for (;;) {
String str = readKeyBoard(2, false);
try {
n = Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.print("数字输入错误,请重新输入:");
}
}
return n;
}
/**
* 从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。 如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
*/
public static int readInt(int defaultValue) {
int n;
for (;;) {
String str = readKeyBoard(2, true);
if (str.equals("")) {
return defaultValue;
}
try {
n = Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.print("数字输入错误,请重新输入:");
}
}
return n;
}
/**
* 从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。
*/
public static String readString(int limit) {
return readKeyBoard(limit, false);
}
/**
* 从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。 如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
*/
public static String readString(int limit, String defaultValue) {
String str = readKeyBoard(limit, true);
return str.equals("") ? defaultValue : str;
}
/**
* 用于确认选择的输入。该方法从键盘读取‘Y’或’N’,并将其作为方法的返回值。
*/
public static char readConfirmSelection() {
char c;
for (;;) {
String str = readKeyBoard(1, false).toUpperCase();
c = str.charAt(0);
if (c == 'Y' || c == 'N') {
break;
} else {
System.out.print("选择错误,请重新输入:");
}
}
return c;
}
private static String readKeyBoard(int limit, boolean blankReturn) {
String line = "";
while (scanner.hasNextLine()) {
line = scanner.nextLine();
if (line.length() == 0) {
if (blankReturn)
return line;
else
continue;
}
if (line.length() < 1 || line.length() > limit) {
System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
continue;
}
break;
}
return line;
}
}
// Customer.java
package com.water.project02;
public class Customer {
private String name;
private char gender;
private int age;
private String phone;
private String 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;
}
public Customer() {
}
}
// CustomerList.java
package com.water.project02;
public class CustomerList {
private Customer[] customers;
private int total; // 现有客户数量
public CustomerList(int totalCustomer) {
this.customers = new Customer[totalCustomer];
}
public boolean addCustomer(Customer customer) {
if (total >= this.customers.length) {
return false;
}
this.customers[this.total] = customer;
this.total += 1;
return true;
}
public boolean replaceCustomer(int index,Customer customer) {
if (index >= total || index < 0){
return false;
}
this.customers[index] = customer;
return true;
}
public boolean daleteCustomer(int index) {
if (index >= total || index < 0){
return false;
}
for(;index < this.total;index++) {
this.customers[index] = this.customers[index + 1];
}
this.total -= 1;
return true;
}
public Customer[] getAllCustomers() {
Customer[] customersTotal = new Customer[this.total];
for(int i = 0;i<this.total;i++) {
customersTotal[i] = this.customers[i];
}
return customersTotal;
}
public Customer getCustomer(int index) {
if (index >= total || index < 0){
return null;
}
return customers[index];
}
public int getTotal() {
return this.total;
}
}
// CustomerView.java
package com.water.project02;
public class CustomerView {
public static void main(String[] args) {
CustomerView custView = new CustomerView();
custView.enterMainMenu();
}
private CustomerList customerList = new CustomerList(10);
private CMUtility cmu = new CMUtility();
public void enterMainMenu() {
while (true) {
printMenu();
char operation = cmu.readMenuSelection();
System.out.println();
switch (operation) {
case '1':
this.addNewCustomer();
break;
case '2':
this.modifyCustomer();
break;
case '3':
this.deleteCustomer();
break;
case '4':
this.listAllCustomers();
break;
case '5':
return;
}
}
}
private void printMenu() {
System.out.println("-----------------客户信息管理软件-----------------");
System.out.println();
System.out.println(" 1 添 加 客 户");
System.out.println(" 2 修 改 客 户");
System.out.println(" 3 删 除 客 户");
System.out.println(" 4 客 户 列 表");
System.out.println(" 5 退 出");
System.out.println();
System.out.print("\t 请选择(1-5):");
}
private void addNewCustomer() {
System.out.println("---------------------添加客户---------------------");
Customer customer = new Customer();
System.out.print("姓名:");
customer.setName(cmu.readString(10));
System.out.print("性别:");
customer.setGender(cmu.readChar());
System.out.print("年龄:");
customer.setAge(cmu.readInt());
System.out.print("电话:");
customer.setPhone(cmu.readString(20));
System.out.print("邮箱:");
customer.setEmail(cmu.readString(30));
System.out.println();
if (customerList.addCustomer(customer)) {
System.out.println("---------------------添加完成---------------------\n");
} else {
System.out.println("---------------------添加失败---------------------\n");
}
}
private void modifyCustomer() {
System.out.println("---------------------修改客户---------------------");
System.out.print("请选择待修改客户编号(-1退出):");
int index = cmu.readInt();
if (index == -1) {
return;
}
index -= 1; // 编号从1开始
Customer customer = new Customer();
Customer customerOld = customerList.getCustomer(index);
System.out.print("姓名(" + customerOld.getName() + "):");
customer.setName(cmu.readString(10, customerOld.getName()));
System.out.print("性别(" + customerOld.getGender() + "):");
customer.setGender(cmu.readChar(customerOld.getGender()));
System.out.print("年龄(" + customerOld.getAge() + "):");
customer.setAge(cmu.readInt(customerOld.getAge()));
System.out.print("电话(" + customerOld.getPhone() + "):");
customer.setPhone(cmu.readString(20, customerOld.getPhone()));
System.out.print("邮箱(" + customerOld.getEmail() + "):");
customer.setEmail(cmu.readString(30, customerOld.getEmail()));
if (customerList.replaceCustomer(index, customer)) {
System.out.println("---------------------修改完成---------------------\n");
} else {
System.out.println("---------------------修改失败---------------------\n");
}
}
private void deleteCustomer() {
System.out.println("---------------------删除客户---------------------");
System.out.print("请选择待删除客户编号(-1退出):");
int index = cmu.readInt();
System.out.print("确认是否删除(Y/N):");
char flag = cmu.readConfirmSelection();
if (flag == 'N') {
return;
}
index -= 1;
if (customerList.daleteCustomer(index)) {
System.out.println("---------------------删除成功---------------------\n");
} else {
System.out.println("---------------------删除失败---------------------\n");
}
}
private void listAllCustomers() {
System.out.println("---------------------客户列表---------------------\n");
System.out.println("编号\t姓名\t性别\t年龄\t电话\t邮箱");
int total = customerList.getTotal();
Customer[] customers = new Customer[total];
customers = customerList.getAllCustomers();
for (int i = 1; i <= total; i++) {
System.out.println(i + "\t" + customers[i - 1].getName() + "\t" + customers[i - 1].getGender() + "\t"
+ customers[i - 1].getAge() + "\t" + customers[i - 1].getPhone() + "\t"
+ customers[i - 1].getEmail() + "\n");
}
System.out.println("--------------------客户列表完成--------------------\n");
}
}
测试结果
在命令行对的挺齐的,到这就有点错位
-----------------客户信息管理软件-----------------
1 添 加 客 户
2 修 改 客 户
3 删 除 客 户
4 客 户 列 表
5 退 出
请选择(1-5):1
---------------------添加客户---------------------
姓名:qwer
性别:男
年龄:33
电话:12345
邮箱:qwer@
---------------------添加完成---------------------
-----------------客户信息管理软件-----------------
1 添 加 客 户
2 修 改 客 户
3 删 除 客 户
4 客 户 列 表
5 退 出
请选择(1-5):1
---------------------添加客户---------------------
姓名:abcd
性别:女
年龄:55
电话:54321
邮箱:asd@rrr
---------------------添加完成---------------------
-----------------客户信息管理软件-----------------
1 添 加 客 户
2 修 改 客 户
3 删 除 客 户
4 客 户 列 表
5 退 出
请选择(1-5):1
---------------------添加客户---------------------
姓名:tre
性别:男
年龄:66
电话:43532
邮箱:fds@gfd
---------------------添加完成---------------------
-----------------客户信息管理软件-----------------
1 添 加 客 户
2 修 改 客 户
3 删 除 客 户
4 客 户 列 表
5 退 出
请选择(1-5):4
---------------------客户列表---------------------
编号 姓名 性别 年龄 电话 邮箱
1 qwer 男 33 12345 qwer@
2 abcd 女 55 54321 asd@rrr
3 tre 男 66 43532 fds@gfd
--------------------客户列表完成--------------------
-----------------客户信息管理软件-----------------
1 添 加 客 户
2 修 改 客 户
3 删 除 客 户
4 客 户 列 表
5 退 出
请选择(1-5):2
---------------------修改客户---------------------
请选择待修改客户编号(-1退出):1
姓名(qwer):
性别(男):
年龄(33):22
电话(12345):33333
邮箱(qwer@):
---------------------修改完成---------------------
-----------------客户信息管理软件-----------------
1 添 加 客 户
2 修 改 客 户
3 删 除 客 户
4 客 户 列 表
5 退 出
请选择(1-5):3
---------------------删除客户---------------------
请选择待删除客户编号(-1退出):2
确认是否删除(Y/N):n
-----------------客户信息管理软件-----------------
1 添 加 客 户
2 修 改 客 户
3 删 除 客 户
4 客 户 列 表
5 退 出
请选择(1-5):1
---------------------添加客户---------------------
姓名:iouy
性别:男
年龄:3
电话:33367
邮箱:feds@sd
---------------------添加完成---------------------
-----------------客户信息管理软件-----------------
1 添 加 客 户
2 修 改 客 户
3 删 除 客 户
4 客 户 列 表
5 退 出
请选择(1-5):4
---------------------客户列表---------------------
编号 姓名 性别 年龄 电话 邮箱
1 qwer 男 22 33333 qwer@
2 abcd 女 55 54321 asd@rrr
3 tre 男 66 43532 fds@gfd
4 iouy 男 3 33367 feds@sd
--------------------客户列表完成--------------------
-----------------客户信息管理软件-----------------
1 添 加 客 户
2 修 改 客 户
3 删 除 客 户
4 客 户 列 表
5 退 出
请选择(1-5):3
---------------------删除客户---------------------
请选择待删除客户编号(-1退出):2
确认是否删除(Y/N):y
---------------------删除成功---------------------
-----------------客户信息管理软件-----------------
1 添 加 客 户
2 修 改 客 户
3 删 除 客 户
4 客 户 列 表
5 退 出
请选择(1-5):4
---------------------客户列表---------------------
编号 姓名 性别 年龄 电话 邮箱
1 qwer 男 22 33333 qwer@
2 tre 男 66 43532 fds@gfd
3 iouy 男 3 33367 feds@sd
--------------------客户列表完成--------------------
-----------------客户信息管理软件-----------------
1 添 加 客 户
2 修 改 客 户
3 删 除 客 户
4 客 户 列 表
5 退 出
请选择(1-5):5
面向对象(接day10)
面向对象特征之二:继承性
面向对象特征之一day10
- 为描述和处理个人信息,定义类Person:
class Person {
public String name;
public int age;
public Date birthDate;
public String getInfo() {
//...
}
}
- 为描述和处理学生信息,定义类Student:
class Student {
public String name;
public int age;
public Date birthDate;
public String school;
public String getInfo() {
// ...
}
}
- 通过继承,简化Student类的定义:
class Person {
public String name;
public int age;
public Date birthDate;
public String getInfo() {
// ...
}
}
class Student extends Person {
public String school;
}
Student类继承了父类Person的所有属性和方法,并增加了一个属性school。Person中的属性和方法,Student都可以使用
-
为什么要有继承?
- 多个类中存在相同属性和行为时,将这些内容抽取到单独一个类中,那么多个类无需再定义这些属性和行为,只要继承那个类即可
-
此处的多个类称为子类(派生类),单独的这个类称为父类(基类 或超类)
-
类继承语法规则:
class Subclass extends SuperClass{ } -
作用:
- 继承的出现减少了代码冗余,提高了代码的复用性
- 继承的出现,更有利于功能的扩展
- 继承的出现让类与类之间产生了关系,提供了多态的前提
-
注意:不要仅为了获取其他类中某个功能而去继承
-
体现:
- 子类继承了父类,就继承了直接父类和所有间接父类的全部方法和属性(包括private的)
- 在子类中,可以使用父类中定义的方法和属性,也可以创建新的数据和方法
- 在Java 中,继承的关键字用的是“extends”,即子类不是父类的子集,而是对父类的“扩展”
-
关于继承的规则:
- 子类不能直接访问父类中私有的(private)的成员变量和方法
- 子类继承了父类的私有成员变量和方法,只是由于封装性的原因,不能直接访问
-
继承性的规定
- Java只支持单继承和多层继承(父类的父类),不允许多重继承(多个直接父类)
- 一个子类只能有一个父类
- 一个父类可以派生出多个子类
- Java只支持单继承和多层继承(父类的父类),不允许多重继承(多个直接父类)
- 如果我们没有显式的声明一个类的父类的话,则此类继承于java.lang.Object类
- 所有的java类(除java.lang.Object类之外)都直接或间接的继承于java.lang.Object类
- 意味着,所有的java类具有java.lang.Object类声明的功能。