软件设计之Java入门视频(9)
视频教程来自B站尚硅谷:
尚硅谷Java入门视频教程,宋红康java基础视频
相关文件资料(百度网盘)
提取密码:8op3
idea 下载可以关注 软件管家 公众号
学习内容:
该视频共分为1-717部分
本次内容涉及240-269
在写代码时,总是需要来回切换界面来看代码要求,这里推荐Snipaste,可以把截图以窗口形式放在屏幕上
记录内容:
- import关键字
- idea 常用快捷键
- 项目2:客户信息管理软件
- 继承
1、import关键字
1)在源文件中显式的使用import结构导入指定包下的
类、接口
2)声明在包的声明
和类的声明
之间
3)如果需要导入多个结构,则并列写出即可
4)可以使用"xxx.*"
的方式,表示可以导入xxx包下的所有结构
5)如果使用的类或接口是java.lang包下定义的,则可以省略import结构(Java中提供的API)
6)如果使用的类或接口是本包下定义的,则可以省略import结构
7)如果在源文件中,使用了不同包下的的同名的类,则必须以全类名的方式
显示
8)使用"xxx.*"
方式可以调用xxx包下的所有结构,但是如果使用的是xxx子包下的结构,则仍需要显式导入
9)import static :导入指定类或接口中的静态结构
//全类名的方式显式
com.atguigu.exer3.Account acct1 = new com.atguigu.exer3.Account(1000);
import java.util.*;
import java.util.concurrent.*;
public class Example {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
// 可以直接使用 java.util 包下的 ArrayList 类
ExecutorService executor = Executors.newFixedThreadPool(1);
// 需要显式导入 java.util.concurrent 包下的类
}
}
子包
子包是一个
包的嵌套结构
,即在一个包内创建的另一个包
。子包通常用于更细粒度地组织类和接口。包和子包之间的关系可以看作是父目录和子目录之间的关系。
com 是顶级包
com.example 是 com 包的子包
com.example.utils 和 com.example.data 是 com.example 包的子包
com
└── example
├── utils
└── data
2、idea 常用快捷键
alt + enter 快捷修复
ctrl + / 单行注释记得选中想注释的内容
ctrl + shift + / 多行注释
ctrl + D 复制一行
ctrl + X / ctrl + Y 删除一行
sout + Tab 生成输出语句
alt + ins(insert) 快速为变量构建set和get方法
ctrl + Q 查看方法的注释
史上最全的IDEA快捷键总结
3、项目2:客户信息管理软件
CustomerView代码
package ui;
import bean.Customer;
import service.CustomerList;
import util.CMUtility;
import java.sql.SQLOutput;
public class CustomerView {
CustomerList customerList = new CustomerList(10);
public CustomerView() {
Customer customer = new Customer("wang",'男',23,"131333","[email protected]");
customerList.addCustomer(customer);
}
public void enterMainMenu(){
boolean isFlag = true;
while (isFlag) {
System.out.println("-------------客户信息管理软件-------------");
System.out.println();
System.out.println("\t\t\t" + "1 增加客户");
System.out.println("\t\t\t" + "2 修改客户");
System.out.println("\t\t\t" + "3 删除客户");
System.out.println("\t\t\t" + "4 客户列表");
System.out.println("\t\t\t" + "5 退 出");
System.out.println("\t\t\t" + "请选择(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("确认是否退出(Y/N)");
char isExit = CMUtility.readConfirmSelection();
if (isExit == 'Y'){
isFlag = false;
}
}
}
}
private void addNewCustomer(){
System.out.println("-------------添加客户-------------");
System.out.println("姓名:");
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 = customerList.addCustomer(customer);
if(isSuccess){
System.out.println("-------------添加完成-------------");
}else {
System.out.println("-------------添加失败-------------");
}
}
private void modifyCustomer(){
System.out.println("-------------修改客户-------------");
Customer cust;
int number;
for (;;){
System.out.println("请选择修改客户编号(-1退出)");
number = CMUtility.readInt();
if (number == -1){
return;
}
cust = customerList.getCustomer( number- 1);
if(cust ==null){
System.out.println("无法找到指定客户!");
}else {
break;
}
}
System.out.println("姓名(" + cust.getName() + "):");
String name = CMUtility.readString(10,cust.getName());
System.out.println("性别(" + cust.getGender() + "):");
char gender = CMUtility.readChar(cust.getGender());
System.out.println("年龄(" + cust.getAge() + "):");
int age = CMUtility.readInt(cust.getAge());
System.out.println("电话(" + cust.getName() + "):");
String phone = CMUtility.readString(13,cust.getPhone());
System.out.println("邮件(" + cust.getName() + "):");
String email = CMUtility.readString(30,cust.getEmail());
Customer newCust = new Customer(name,gender,age,phone,email);
boolean isReplaced = customerList.replaceCustomer(number-1,newCust);
if (isReplaced ){
System.out.println("-------------修改完成-------------");
}else {
System.out.println("-------------修改失败-------------");
}
}
private void deleteCustomer(){
int number;
Customer customer;
System.out.println("-------------删除客户-------------");
for (;;){
System.out.println("请选择删除客户编号(-1退出)");
number = CMUtility.readInt();
if (number == -1){
return;
}
customer = customerList.getCustomer( number- 1);
if(customer ==null){
System.out.println("无法找到指定客户!");
}else {
break;
}
}
System.out.println("确认是否删除(Y/N):");
char isDelete = CMUtility.readConfirmSelection();
if (isDelete =='Y') {
boolean deleteSuccess = customerList.deleteCustomer(number-1);
if(deleteSuccess){
System.out.println("-------------删除成功-------------");
}else {
System.out.println("-------------删除失败-------------");
}
}
}
private void listAllCustomers(){
System.out.println("-------------客户列表-------------");
int total = customerList.getTotal();
if (total == 0){
System.out.println("没有客户记录!");
}else {
System.out.println("编号\t姓名\t性别\t年龄\t电话\t邮箱");
Customer[] custs = 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 customerView = new CustomerView();
customerView.enterMainMenu();
}
}
Customer代码
package bean;
/*
@Description Customer 为实体对象,用来封装客户信息
*/
public class 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;
}
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;
}
}
CustomerList代码
package service;
import bean.Customer;
public class CustomerList {
private Customer[] customers;
private int total = 0;
public CustomerList(int totalCustomer){
customers = new Customer[totalCustomer];
}
public boolean addCustomer(Customer customer){
if (total>=customers.length){
return false;
}else{
customers[total] = customer;
total ++;
return true;
}
}
public boolean replaceCustomer(int index, Customer cust){
if (index <0 ||index >=total){
return false;
}else{
customers[index] = cust;
return true;
}
}
public boolean deleteCustomer(int index) {
if (index < 0 || index >= total) {
return false;
} else {
for (int i = index; i < total - 1; i++) {
customers[i] = customers[i + 1];
}
customers[total - 1] = null;
total--;
return true;
}
}
public Customer[] getAllCustomers(){
Customer[] custs = new Customer[total];
for (int i = 0; i<total;i++){
custs[i] = customers[i];
}
return custs;
}
public Customer getCustomer(int index){
if (index <0 || index>=total ){
return null;
}else {
return customers[index];
}
}
public int getTotal(){
return total;
}
}
4、继承
继承性的好处:
1)减少代码冗余,提高代码复用性
2)便于功能的扩展
3)为之后多态性的使用,提供前提
继承性的格式:
class A extends B{}
A:子类、派生类、subclass
B:父类、超类、基类、superclass
体现:一旦子类A继承父类B后,子类A中就获取了父类B中声明的结构:属性、方法
特别的:父类中声明为private的属性或方法
,子类继承父类以后,仍然认为获取了父类中的私有结构
,只是因为封装性
的影响,使得子类不能直接调用父类
继承性的规定
标签:Customer,Java,入门,软件设计,System,-------------,println,public,out From: https://blog.csdn.net/thm19990903/article/details/1401003621)一个类可以被多个子类继承
2)Java中类的单继承性:一个类只能有一个父类
3)子类父类是相对概念:A既可以是B的子类,又可以是C的父类
4)子类直接继承的父类,称为:直接父类
;间接继承的父类称作间接父类
5)子类继承父类以后,就获取了直接父类以及所有间接父类中声明的属性和方法