代码
package homework;
import java.util.Scanner;
abstract class person {
int age ;
String name;
int sex;
}
interface get{
void get(); //获取信息
}
interface print { //输出信息
void print();
}
class employee extends person implements get,print{
public void get(){
Scanner scanner = new Scanner(System.in);
try { //依次输入信息
this.name=scanner.nextLine();
this.age=scanner.nextInt();
this.sex= scanner.nextInt();
} catch (Exception e) {
System.out.println("输入有误"); //如遇见输入错误则报错
}
}
public void print() { //输出员工信息
if(this.sex==1) System.out.println(this.name+"是男性员工,今年"+this.age+"岁");
else System.out.println(this.name+"是女性员工,今年"+this.age+"岁");
}
}
public class homework {
public static void main(String args[]) {
employee employee1 = new employee();
employee employee2 = new employee();
employee employee3 = new employee();
Scanner scanner = new Scanner(System.in);
//输入信息
System.out.println("请以此输入员工的姓名、年龄与性别(1为男性,0为女性)");
employee1.get();
employee2.get();
employee3.get();
//输出信息
employee1.print();
employee2.print();
employee3.print();
}
}
设计思路
不知道如何表达,想设计一个读取并输出员工信息的内容。抽象了一个person 类,然后用get接口实现读取,print接口实现输出
用一个employee继承person类
困难与解决
在输入是发现错误用try,catch语句解决
心得
public修饰的变量或方法可以被其他类和方法访问。
private修饰的变量和方法只对自己所在的方法或者类可见。
每个类的成员变量对其他类是私有的,方法中的局部变量对该类中的其他方法是私有的。
将项目拆分成小的方法与步骤,操作起来思路会清晰许多。