
public class Three {
public static void main(String[] args) {
Student stu = new Student("Zhang San", true, (short)12)
System.out.println("Student name: " + stu.name)
System.out.println("Student is a male?: " + stu.sex)
System.out.println("Student's age: " + stu.age)
stu.work()
stu.study()
Teacher teacher = new Teacher()
teacher.learnMoney()
}
}
abstract class Person{//抽象类Person
protected String name
protected boolean sex
protected short age
protected abstract void work()//work抽象方法
}
interface Learnmoney{//Learnmoney接口
public void learnMoney()
}
interface Study{//Study接口
public void study()
}
class Student extends Person implements Study{//Student类
public void work() {
System.out.println("学生的工作是努力学习")
}
public Student(String name, boolean sex, short age){
super.name = name
super.sex = sex
super.age = age
}
public void study() {
System.out.println("学生正在学习")
}
}
class Teacher extends Person implements Learnmoney{
public void work() {
System.out.println("教师的工作是教书育人")
}
public void learnMoney() {
System.out.println("教师正在赚钱")
}
}
class Docotor extends Person implements Learnmoney{
public void work() {
System.out.println("医生的职责是救死扶伤")
}
public void learnMoney() {
System.out.println("医生正在赚钱")
}
}
------------------------------------
4文件名:Four.java
public class Four {
public static void main(String[] args) {
Rectangle r = new Rectangle(3, 4)
System.out.println("Area is : " + r.area())
System.out.println("Circle is: " + r.circle())
}
}
class Rectangle{
private double width
private double height
public Rectangle(double width, double height){
this.width = width
this.height = height
}
public double circle(){//求周长
return (width + height) * 2
}
public double area(){//求面积
return width * height
}
}
--------------------
5Five.java
public class Five {
public static void main(String[] args) {
AImpl a = new AImpl()
a.paint()
}
}
interface A {
public int method1(int x)
public int method2(int x, int y)
}
class AImpl implements A{
public int method1(int x) {
return (int)Math.pow(x, 5)
}
public int method2(int x, int y) {
return x >y? x: y
}
public void paint(){
int result1 = method1(2)
int result2 = method2(2, 8)
System.out.println("method1(2) = " + result1)
System.out.println("method2(2, 8) = " + result2)
}
}
-----------------------------测试
method1(2) = 32
method2(2, 8) = 8
第一题看的脑壳疼,但是和第二题的意思差不多,我帮你做了第二题
public class List {public static void main(String[] args) {
Employee e1 = new Employee("张强","2010/09/01",6890)
e1.show("普通")
System.out.println("年纳税:"+e1.tax())
Manager m1 = new Manager("朱慧","2003/07/06",18530,38000)
m1.show("管理")
System.out.println("年纳税:"+m1.tax())
}
}
//下面是补全的代码
class Employee{
String name,date
double salary,bonus
Employee(String name,String date,double salary){
this.name = name
this.date = date
this.salary = salary
}
public void show(String str){
System.out.println("岗位:"+str)
System.out.println("姓名:"+name+",入职时间:"+date+",月薪:"+salary)
}
public double tax(){
if(salary <= 0){
throw new RuntimeException("工资不允许小于等于0")
}
else if(salary>3000 && salary<=5000){
salary = salary*0.05*12 //纳税这里我也不知道他们具体是怎么个算法,反正意思差不多,套进去就行了
}
else if(salary>5000 && salary<=10000){
salary = salary*0.1*12
}
else if(salary>10000 && salary<=15000){
salary = salary*0.15*12
}
else if(salary>15000){
salary = salary*0.2*12
}
return salary+(bonus*0.2)
}
}
class Manager extends Employee{
Manager(String name,String date,double salary,double bonus){
super(name,date,salary)
super.bonus = bonus
}
public void show(String str){
System.out.println("岗位:"+str)
System.out.println("姓名:"+name+",入职时间:"+date+",月薪:"+salary+",奖金:"+bonus)
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)