
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
1-4:has-a的关系、contains-a的关系、is-a的关系;
“班级”与“学生”是has-a的关系;
“学生”与“大学生”是is-a的关系。
1-5:
正确,把两个看做是类,“清华大学”包含了“大学”的全部成员,“大学”能接受的信息,“清华大学”一样能接受到。
1-6:
过程:面向对象的分析、面向对象的设计、面向对象的实现;
5个层次:对象-类层、静态属性层、服务层、结构层 、主题层;
引入了:界面管理、任务管理、数据管理。
PS:其实我也是JAVA新手,不知答得对不对,希望这些能对你有帮助,呵呵。。。。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)