1.定义一个ClassName接口,接口中只有一个抽象方法getClassName();设计一个类Company,该类实现接口ClassName中的方法getClassName(),功能是获取该类的类名称,编写应用程序使用Company类
//================================================= // File Name : ClassName_demo //------------------------------------------------------------------------------ // Author : Common // 接口名:ClassName // 属性: // 方法: interface ClassName{ public String getClassName(); //抽象方法 } //类名:Company //属性: //方法: class Company implements ClassName{ private String name; public Company(String name){ this.name = name; } @Override public String getClassName() { // TODO 自动生成的方法存根 return name; } public void print(){ System.out.println(this.getClassName()); } } //主类 //Function : ClassName_demo public class ClassName_demo { public static void main(String[] args) { // TODO 自动生成的方法存根 Company com = new Company("百度"); com.print(); } }
4.定义员工类,具有姓名、年龄、性别属性,并具有构造方法和显示数据方法。定义管理层类,继承员工类,并有自己的属性职务和年薪。定义职员类,继承员工类,并有自己的属性所属部门和月薪。
//================================================= // File Name : Employee_demo2 //------------------------------------------------------------------------------ // Author : Common // 类名:employee // 属性: // 方法: class employee{ private String name; private int age; private String sex; public employee(String name, int age, String sex) { //构造方法 this.setName(name); this.setAge(age); this.setSex(sex); } public String getName() { return name; } public void setName(String name) { this.name = name; } 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 String Info(){ return (this.getName()+this.getAge()+this.getSex()); } } //类名:Manager //属性: //方法: class Manager extends employee{ private String duty; private int salary_year; public Manager(String name, int age, String sex,String duty,int salary) { super(name, age, sex); // TODO 自动生成的构造函数存根 this.duty = duty; this.salary_year = salary; } public String getDuty() { return duty; } public void setDuty(String duty) { this.duty = duty; } public int getSalary_year() { return salary_year; } public void setSalary_year(int salary_year) { this.salary_year = salary_year; } public String Info(){ return (super.Info()+this.duty+this.salary_year); } } //类名:Staff //属性: //方法: class Staff extends employee{ private String department; private int salary_month; public Staff(String name, int age, String sex,String department,int salary) { super(name, age, sex); // TODO 自动生成的构造函数存根 this.department = department; this.salary_month = salary; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public int getSalary_month() { return salary_month; } public void setSalary_month(int salary_month) { this.salary_month = salary_month; } public String Info(){ return (super.Info()+this.department+this.salary_month); } } //主类 //Function : Employee_demo2 public class Employee_demo2 { public static void main(String[] args) { // TODO 自动生成的方法存根 Manager man = new Manager("张三",18,"男","管理",10000); Staff sta = new Staff("李四",18,"男","开发",1000); System.out.println(man.Info()); System.out.println(sta.Info()); } }
定义类Shape,用来表示一般二维图形,Shape具有抽象方法area和perimeter,分别用来极端形状的面积和周长。
//================================================= // File Name : Shape_demo //------------------------------------------------------------------------------ // Author : Common // 类名:Shape // 属性: // 方法: abstract class Shape{ public abstract float area(); public abstract float perimeter(); } //类名:rec //属性: //方法: class rec extends Shape{ private float Length; private float width; public rec(float length, float width) { this.Length = length; this.width = width; } @Override public float area() { // TODO 自动生成的方法存根 return (this.Length * this.width); } @Override public float perimeter() { // TODO 自动生成的方法存根 return 2 * (this.Length + this.width); } } //主类 //Function : Shape_demo public class Shape_demo { public static void main(String[] args) { // TODO 自动生成的方法存根 rec r = new rec(10.0f,15.0f); System.out.println("area="+r.area()); System.out.println("perimeter="+r.perimeter()); } }