第一题
/*
author:kuokuo
*/
// 父类 Base
class Base{
public Base(){
System.out.println("Base");
}
}
// 子类Child继承父类Child
class Child extends Base{
public Child(){
System.out.println("Child");
}
}
//测试
class sample{
public static void main(String[] args) {
Child c=new Child();
}
}
结果
Base
Child
进程已结束,退出代码为 0
第二题
import java.util.WeakHashMap;
/*
author:Kuo
*/
//父类Animal
abstract class Animal {
private String color;
private String type;
private int age;
private int weight;
public String getColor(){
return color;
}
public void setColor(String color){
this.color=color;
}
public String gettype(){
return type;
}
public void setType(String type){
this.type=type;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age=age;
}
public int getWeight(){
return weight;
}
public void setWeight(int Weight){
this.weight=weight;
}
public abstract void info(); //方法
}
import unit7.test2.Animal;
/*
author:Kuo
*/
//子类Fish
public class Fish extends Animal {
public String getColor(){
return super.getColor();
}
public void setColor(String color){
super.setColor(color);
}
public String getType(){
return gettype();
}
public void setType(String type){
super.setType(type);
}
public int getAge(){
return getAge();
}
public void setAge(int age){
super.setAge(age);
}
public int getWeight(){
return getWeight();
}
public void setWeight(int age){
super.setWeight(age);
}
public void info(){
System.out.println("我是一只"+getWeight()+"重的鱼");
System.out.println("今年"+getAge()+"岁了");
}
}
import unit7.test2.Animal;
/*
author:Kuo
*/
//子类Bird
public class Bird extends Animal {
public String getColor(){
return super.getColor();
}
public void setColor(String color){
super.setColor(color);
}
public String getType(){
return super.gettype();
}
public void setType(String type){
super.setType(type);
}
public int getAge(){
return super.getAge();
}
public void setAge(int age){
super.setAge(age);
}
public int getWeight(){
return super.getWeight();
}
public void setWeight(int weight){
super.setWeight(weight);
}
public void info(){
System.out.println("我是一只"+getColor()+"的"+gettype());
System.out.println("今年"+getAge()+"岁了");
}
}
结果
我是一只红色的鸟
今年4岁了
我是一只红色的鱼
今年2岁了
进程已结束,退出代码为 0
第三题
/*
author:Kuo
*/
//父类Phone
abstract class Phones {
public abstract void call();
}
class BaseStation{
public Phones callPhone(String type){
if (type.equals("IPhone")){
return new IPhone();
}
else if (type.equals("APhone")){
return new APhone();
}
else {
return new WPhone();
}
}
}
/*
author:Kuo
实现方法
*/
class IPhone extends Phones { //IPhone继承Phone
public void call() {
System.out.println("苹果手机打电话");
}
}
class APhone extends Phones { //APhone继承Phone
public void call() {
System.out.println("安卓手机打电话");
}
}
class WPhone extends Phones{ //WPhone继承Phone
public void call(){
System.out.println("Windows Phone 手机打电话" );
}
}
/*
author:Kuo
测试打电话
*/
public class User {
static Scanner a = new Scanner(System.in);
public static void main(String[] args) {
boolean flag=false;
String b;
BaseStation c = new BaseStation();
String d;
do {
System.out.println("请问用什么手机打电话?1.苹果手机 2.安卓手机 3.Windous Phone手机");
b=a.next();
if ("1".equals(b)){
d="IPhone";
}
else if ("2".equals(b)){
d="APhone";
}
else {
d="WPhone";
}
Phones phones=c.callPhone(d);
phones.call();
System.out.println("继续吗?按n退出,按其他任意字符继续");
b=a.next();
if ("n".equals(b)){
flag=true;
}
}while (!flag);
System.out.println("再见");
}
}
结果
请问用什么手机打电话?1.苹果手机 2.安卓手机 3.Windous Phone手机
1
苹果手机打电话
继续吗?按n退出,按其他任意字符继续
2
请问用什么手机打电话?1.苹果手机 2.安卓手机 3.Windous Phone手机
3
Windows Phone 手机打电话
继续吗?按n退出,按其他任意字符继续
n
再见
进程已结束,退出代码为 0