3, 文件名:Three.java
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
第一题
abstract class Person{
public String name;
public String sex;
public int age;
abstract void work();
}
interface Learnmoney{
void Learnmoney();
}
interface Study{
void study();
}
class Docotor extends Person implements Learnmoney{
void work() {
System.out.println("我是一个Docotor");
}
public void Learnmoney(){}
}
class Teacher extends Person implements Learnmoney{
String name="王老师";
String sex="女";
int age=30;
void work(){
System.out.println("我是一个teacher");
}
public void Learnmoney() {
System.out.println("同学们要交学费了");
}
}
class Student extends Person implements Study{
String name="李同学";
String sex="男";
int age=16;
void work(){
System.out.println("我是一个student");
}
public void study() {
System.out.println("我要好好学习");
}
}
public class stu {
stu(Teacher t,Student s) {
t = new Teacher();
s = new Student();
t.work();
System.out.println(t.name+"的性别是"+t.sex+",年龄是"+t.age );
t.Learnmoney();
s.work();
System.out.println(s.name+"的性别是"+s.sex+",年龄是"+s.age );
s.study();
}
public static void main(String[] args) {
Teacher t = null;
Student s = null;
stu st=new stu(t, s);
}
}
第二题
import java.util.Scanner;
public class Rectangle {
public static int width;
public static int height;
int length(int width,int height){
int l;
l=width*2+height*2;
return l;
}
int area(int width,int height){
int a;
a=width*height;
return a;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("请输入长:");
Scanner sc = new Scanner(System.in);
String l = sc.next();
width=Integer.parseInt(l);
System.out.println("请输入宽:");
String h = sc.next();
height=Integer.parseInt(h);
Rectangle r=new Rectangle();
int length=r.length(width,height);
int area = r.area(width,height);
System.out.print("你所输入的矩形的周长是:"+length);
System.out.print("你所输入的矩形的面积是:"+area);
}
}
第三题
interface A{
int method1(int x);
int method2(int x,int y);
}
public class B implements A {
public static void main(String[] args) {
B b=new B();
System.out.println(b.method1(2));
System.out.println(b.method2(2,8));
}
public int method1(int x) {
int z=(int) Math.pow(5, x);
return z;
}
public int method2(int x, int y) {
return Math.max(x, y);
}
}
第三题:
//抽象类Person
abstract class Person{
String name;
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public int getAge() {
return age;
}
String sex;
int age;
public Person(String name,String sex,int age){
this.name=name;
this.sex=sex;
this.age=age;
}
public abstract void work();
}
//接口 Learnmoney
interface Learnmoney{
public void learnmoney();
}
//接口 Study
interface Study{
public void study();
}
//Teacher类
class Teacher extends Person implements Learnmoney{
public Teacher(String name, String sex, int age) {
super(name, sex, age);
}
public void work(){
System.out.println("我是老师,我工作是授课!");
}
public void learnmoney() {
System.out.println("我是老师,我工作是挣钱的!");
}
}
//类Docotor
class Docotor extends Person implements Learnmoney{
public Docotor(String name, String sex, int age) {
super(name, sex, age);
}
public void work(){
System.out.println("我是医生,我工作是治病!");
}
public void learnmoney() {
System.out.println("我是医生,我工作是挣钱的!");
}
}
//类Student
class Student extends Person implements Study{
public Student(String name, String sex, int age) {
super(name, sex, age);
}
public void work(){
System.out.println("我是学生,我工作是学习知识!");
}
public void study() {
System.out.println("我是学生,我主要是学习!");
}
}
//测试类
public class TestPerson {
public static void main(String []args){
//创建两个类对象
Student xiaoming=new Student("小明","男",18);
Teacher wang=new Teacher("王老师","男",29);
xiaoming.work();
xiaoming.study();
wang.work();
wang.learnmoney();
}
}
不想写了
问题其实很简单,只是量太多了