如何使用抽象类?
抽象类用abstract修饰,抽象类中一旦有抽象方法,子类必须全部重写,否则也定义为抽象类
注意事项:
具体事物:基础班学员,就业班学员
共性:姓名,学习,休假。
code
public abstract class Student {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public abstract void study();
public abstract void vacation();
}
package com.day08;
public class BasicClassStudent extends Student{
@Override
public void study() {
System.out.println("基础班学生学习");
}
@Override
public void vacation() {
System.out.println("基础班学生放假");
}
}
package com.day08;
public class EmploymentClassStudent extends Student{
@Override
public void study() {
System.out.println("就业班学生学习");
}
@Override
public void vacation() {
System.out.println("就业班学生放假");
}
}
package com.day08;
public class StudentTest {
public static void main(String[] args) {
BasicClassStudent student1 = new BasicClassStudent();
EmploymentClassStudent student2 = new EmploymentClassStudent();
student1.setName("小明");
System.out.println(student1.getName());
student1.study();
student1.vacation();
student1.setName("小红");
System.out.println(student1.getName());
student2.study();
student2.vacation();
}
}
接口(interface)名首字母要加一个I
实现接口的类末尾要加Impl
接口中默认属性:public static final
接口中默认方法:public abstract
实现接口的类要实现接口中所有的方法用implements
类既可以继承一个的的同时实现多个接口
如:public class StudentImpl extends Student implements IStudent
具体事物:基础班学员,就业班学员
共性:姓名,学习,休假。
code
package com.day08;
public interface IStudent {
public abstract void name();
public abstract void study();
public abstract void vacation();
}
package com.day08;
public class BasicClassStudentImpl implements IStudent{
@Override
public void name() {
System.out.println("基础班学生小明");
}
@Override
public void study() {
System.out.println("基础班学生学习");
}
@Override
public void vacation() {
System.out.println("基础班学生放假");
}
}
package com.day08;
public class EmploymentClassStudentImpl implements IStudent{
@Override
public void name() {
System.out.println("就业班学生小红");
}
@Override
public void study() {
System.out.println("就业班学生学习");
}
@Override
public void vacation() {
System.out.println("就业班学生放假");
}
}
package com.day08;
public class IStudentTest {
public static void main(String[] args) {
BasicClassStudentImpl student01 = new BasicClassStudentImpl();
EmploymentClassStudentImpl student02 = new EmploymentClassStudentImpl();
student01.name();
student01.study();
student01.vacation();
student02.name();
student02.study();
student02.vacation();
}
}
用接口实现一个登陆注册系统
package com.day08;
public class User {
private int id;
private String name;
private String password;
private int age;
private String sex;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
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;
}
public void print() {
System.out.printf("ID:%d\tName:%s\tPassword:%s\tAge:%d\tSex:%s\n",
id, name, password, age, sex);
}
}
package com.day08;
public interface IFuncation {
public abstract void add();
public abstract void delete();
public abstract void revise();
public abstract void query();
}
package com.day08;
import java.util.Scanner;
public class FuncationImpl implements IFuncation{
private int count = 1;
private Scanner sca = new Scanner(System.in);
public static User[] user = new User[5];
@Override
public void add() {
if(count > 10) {
System.out.println("用户已满,禁止添加0-0");
return ;
}
User temp = new User();
System.out.print("请输入添加的用户名:");
temp.setName(sca.next());
System.out.print("请设置该用户的密码:");
temp.setPassword(sca.next());
System.out.print("请设置该用户的年龄:");
temp.setAge(sca.nextInt());
System.out.print("请设置该用户的性别:");
temp.setSex(sca.next());
count++;
temp.setId(count);
user[count - 1] = temp;
System.out.println("添加成功0-0");
return ;
}
@Override
public void delete() {
if(count < 1) {
System.out.println("用户不可再删,删除失败0-0");
return ;
}
System.out.print("请输入要删除的编号:");
int id = sca.nextInt();
for(int i = 0; i < count; i++) {
if(id == user[i].getId()) {
for(int j = i; j < count - 1; j++) {
user[j] = user[j + 1];
user[j].setId(user[j].getId() - 1);
}
count--;
System.out.println("删除成功0-0");
return ;
}
}
System.out.println("查找不到所输入的编号0-0");
return ;
}
@Override
public void revise() {
System.out.print("请输入您索要修改的编号:");
int id = sca.nextInt();
for(int i = 0; i < count; i++) {
if(id == user[i].getId()) {
System.out.println("请修改以下数据:");
System.out.print("Name:");
user[i].setName(sca.next());
System.out.print("Age:");
user[i].setAge(sca.nextInt());
System.out.print("Sex:");
user[i].setSex(sca.next());
System.out.println("修改成功0-0");
return ;
}
}
System.out.println("查找不到所输入的编号0-0");
return ;
}
@Override
public void query() {
for(int i = 0; i < count; i++) {
user[i].print();
}
}
}
package com.day08;
import java.util.Scanner;
public class LemonSystem {
private static int n;
private static Scanner sca = new Scanner(System.in);
public static void main(String[] args) {
FuncationImpl funcationImpl = new FuncationImpl();
while(true) {
System.out.println("欢迎来到小柠檬系统0-0");
System.out.println("1. 登陆");
System.out.println("2. 注册");
System.out.println("0. 退出");
n = sca.nextInt();
if(n == 1) {
login(funcationImpl);
}
if(n == 2) {
register(funcationImpl);
}
if(n == 0) {
return ;
}
}
}
public static void register(FuncationImpl funcationImpl) {
User temp = new User();
System.out.print("请输入用户名:");
temp.setName(sca.next());
System.out.print("请设置该用户的密码:");
temp.setPassword(sca.next());
System.out.print("请设置该用户的年龄:");
temp.setAge(sca.nextInt());
System.out.print("请设置该用户的性别:");
temp.setSex(sca.next());
temp.setId(1);
FuncationImpl.user[0] = temp;
System.out.println("注册成功0-0");
return ;
}
public static void login(FuncationImpl funcationImpl) {
System.out.print("请输入用户名:");
String name = sca.next();
System.out.print("请输入密码:");
String password = sca.next();
if(funcationImpl.user[0].getName().equals(name) &&
funcationImpl.user[0].getPassword().equals(password)) {
System.out.println("登陆成功0-0");
while(true) {
System.out.println("欢迎进入小柠檬内部系统0-0");
System.out.println("1. 添加");
System.out.println("2. 删除");
System.out.println("3. 修改");
System.out.println("4. 查询");
System.out.println("0. 退出");
n = sca.nextInt();
if(n == 1) {
funcationImpl.add();
}
if(n == 2) {
funcationImpl.delete();
}
if(n == 3) {
funcationImpl.revise();
}
if(n == 4) {
funcationImpl.query();
}
if(n == 0) {
break ;
}
}
return ;
}
System.out.println("登陆失败0-0");
return ;
}
}