1.定义一个ClassName接口,接口中只有一个抽象方法getClassName()。设计一个类Company,该类实现接口ClassName中的方法getClassName(),功能是获取该类的类名称。编写应用程序使用Company类。
代码如下
package www.zbq.com;
interface ClassName {
public abstract void getClassName();
}
class Company implements ClassName {
public void getClassName() {
System.out.println(getClass());
}
}
public class Test {
public static void main(String[] args) {
Company c = new Company();
c.getClassName();
}
}
2.建立一个人类(Person)和学生类(Student)功能要求:
A.Person类中包含4个保护型的数据成员name,address,sex,age分别为字符串,字符串,字符和整形。表示:姓名,地址,性别,年龄。一个四参构造方法,一个无参构造方法,及一个输出方法用于显示四种属性。
B.Student继承Person,并增加输出成母案math,english存放数学和英语成绩。一个六参构造方法,一个两参构造方法,一个无参构造方法,重写输出方法用于显示全部六种属性。
代码如下
package www.zbq.com;
class Person {
public String name;
public String address;
public char sex;
public int age;
public Person() {
}
public Person(String name, String address, char sex, int age) {
this.name=name;
this.address=address;
this.sex=sex;
this.age=age;
}
public void print() {
System.out.println("姓名:" + this.name + "地址:" + this.address + "性别:" + this.sex + "年龄:" + this.age);
}
}
class Student extends Person
{
public double math;
public double english;
public Student() {}
public Student(double math,double english)
{
this.math=math;
this.english=english;
}
public Student(String name,String address,char sex,int age,double math,double english)
{
this.name=name;
this.address=address;
this.sex=sex;
this.age=age;
this.math=math;
this.english=english;
}
public void print()
{
System.out.println("姓名:" + this.name + "地址:" + this.address + "性别:" + this.sex + "年龄:" + this.age+"数学成绩:"+this.math+"英语成绩:"+this.english);
}
}
3.定义员工类,具有姓名,年龄,性别属性,并具有构造方法,显示数据方法,定义管理层类,继承员工类,并有自己的属性:职务,年薪。丁一志愿类,继承员工类,并有自己的属性:所属部门,月薪。
代码如下:
class Employee {
public String name;
public int age;
public String sex;
public void Employee(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public void print() {
System.out.println("姓名:" + this.name + "年龄:" + this.age + "性别:" + this.sex);
}
}
class Management extends Employee {
private String duty;
private int AnnualSalary;
public void Management(String duty, int AnnualSalary) {
this.duty = duty;
this.AnnualSalary = AnnualSalary;
}
public void print() {
System.out.println("姓名:" + this.name + "年龄:" + this.age + "性别:" + this.sex + "职务:" + this.duty + "年薪:"
+ this.AnnualSalary);
}
}
class OfficeCleck extends Employee {
private String department;
private int MonthlySalary;
public void OfficeCleck(String department, int MonthlySalary) {
this.department = department;
this.MonthlySalary = MonthlySalary;
}
public void print() {
System.out.println("姓名:" + this.name + "年龄:" + this.age + "性别:" + this.sex + "部门:" + this.department + "月薪:"
+ this.MonthlySalary);
}
}
public class Test {
public static void main(String[] args) throws Exception{
Management management = new Management();
management.Employee("张三", 26, "男");
management.Management("项目经理", 300000);
OfficeCleck officecleck = new OfficeCleck();
officecleck.Employee("李四", 18, "男");
officecleck.OfficeCleck("项目部", 10000);
management.print();
officecleck.print();
}
}
定义类Shape表示一般二维图形。Shape具有抽象方法area和perimeter,分别计算形状的面积和周长。试定义一些二维形状类(如矩形,三角形,圆形等),这些类均为Shape类的子类。
abstract class Shape {
abstract void area();
abstract void perimeter();
}
class Roundness extends Shape {
public double radius;
public Roundness(double radius) {
this.radius = radius;
}
public void area() {
System.out.println("半径为" + this.radius + "厘米的圆,面积为" + (radius * radius * 3.14));
}
public void perimeter() {
System.out.println("半径为" + this.radius + "厘米的圆,周长为" + (radius * 2 * 3.14));
}
}
class Rectangle extends Shape {
public double height;
public double width;
public Rectangle(double height, double width) {
this.height = height;
this.width = width;
}
public void area() {
System.out.println("高为" + this.height + "宽为" + this.width + "的矩形,面积为" + (this.height * this.width));
}
public void perimeter() {
System.out.println("高为" + this.height + "宽为" + this.width + "的矩形,周长为" + ((this.height + this.width) * 2));
}
}
public class Test {
public static void main(String[] args) {
Roundness roundness = new Roundness(3.5);
roundness.area();
roundness.perimeter();
Rectangle rectangle = new Rectangle(5, 6);
rectangle.area();
rectangle.perimeter();
}
}
因为时间原因,所以只写了圆形的和矩形的,其余的都可以以此类推,以上所有的问题,如果有什么错误,或者有更优解,欢迎各位联系我,感谢各位的阅读。