题目1:
主程序:
package test; import java.util.Scanner; public class Rectangle { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("请输入长宽高:"); double s1 = s.nextDouble(); double s2 = s.nextDouble(); double s3 = s.nextDouble(); Rectangletest sc = new Rectangletest(s1, s2); Cuboid cb = new Cuboid(s1, s2, s3); System.out.println("周长:" + sc.perimeter() + "面积:" + sc.area() + "体积为:" + cb.volume() + "表面积:" + cb.allarea()); } }
父类:
package test; public class Rectangletest { private double length; private double width; public Rectangletest() { this(0.0, 0.0); } public Rectangletest(double length, double width) { this.length = length; this.width = width; } double perimeter() { double p = (this.width + this.length) * 2; return p; } double area() { double a = this.width * this.length; return a; } public double getlength() { return length; } public double getwidth() { return width; } }
子类:
package test; public class Cuboid extends Rectangletest { double height; public Cuboid() { this(0, 0, 0); } public Cuboid(double length, double width, double heigth) { super(length, width); this.height = heigth; } public double allarea() { double ar = ((super.getlength() * super.getwidth()) + (super.getlength() * height) + (super.getwidth() * height)) * 2; return ar; } public double volume() { return super.area() * height; } }
附带源代码:
题目2:
package com; public class A { public int a = 1; private int b = 2; protected int c = 3; int d = 4; public int getA() { return a; } public void setA(int a) { this.a = a; } public int getB() { return b; } public void setB(int b) { this.b = b; } public int getC() { return c; } public void setC(int c) { this.c = c; } public int getD() { return d; } public void setD(int d) { this.d = d; } }
package com; public class B extends A { public static void main(String[] args) { B b1 = new B(); System.out.println(b1.a); // System.out.println(b1.b); //同包不同类,私有不可访问 System.out.println(b1.c); System.out.println(b1.d); } }
package com; public class C { public static void main(String[] args) { A a1 = new A(); System.out.println(a1.a); // System.out.println(a1.b);//同包不同类,私有不可访问 System.out.println(a1.c); System.out.println(a1.d); } }
——————————————–以上为com包—————————————
package org; import com.A; public class D { public static void main(String[] args) { A d1 = new A(); System.out.println(d1.a); // System.out.println(d1.b);//不同包,除共有外均不可直接访问 // System.out.println(d1.c); // System.out.println(d1.d); } }
package org; import com.A; public class E extends A { public static void main(String[] args) { E e1 = new E(); System.out.println(e1.a); // System.out.println(e1.b); //不同包,继承私有不可访问,但可get,如下; System.out.println(e1.getB()); System.out.println(e1.c); // System.out.println(e1.d);//不同包,继承缺省不可访问,但可get,如下; System.out.println(e1.getD()); } }
——————————————–以上为org包—————————————
源码:
题目3:
package castDemo1; class Employee { } class Manager extends Employee { public String toString() { return "i'm a manager."; } }
public class CastDemo1 { public static void main(String[] args) { Employee stuff; Manager boss = new Manager(); stuff = boss; Manager myBoss; myBoss = (Manager) stuff; System.out.println(myBoss); } }
源码:
题目4:
package EdibleTest; interface Ediable { public abstract String howToEat(); } abstract class Food { } abstract class Fruit extends Food implements Ediable { } class Beef extends Food implements Ediable { public String howToEat() { return "Fried Beef Steak"; } } class Mutton extends Food implements Ediable { public String howToEat() { return "Roast Mutton"; } } class Apple extends Fruit { public String howToEat() { return "Make Apple Pie"; } } class Orange extends Fruit { public String howToEat() { return "Make Orange Juice"; } } public class EdibleTest { public static void main(String args[]) { Object[] obj = { new Beef(), new Mutton(), new Apple(), new Orange() }; for (int i = 0; i < obj.length; i++) { if (obj[i] instanceof Ediable) System.out.println(((Ediable) (obj[i])).howToEat()); } } }
源码:
本站由以下主机服务商提供服务支持:
0条评论