抽象类与继承相关练习(java)

(一)、使用抽象类和抽象方法模拟不同人种
世界上有很多人种,例如中国人、美国人等,他们所说的语言各不相同。不管是什么人种,他们都是人类
实现步骤:
 (1) 创建一个抽象类Person,属性:name 和age
 (2) 在Person 类中定义一个抽象方法say()
 (3) 创建Chinese 类继承Person
 (4) 创建American 类继承Person
package aaaw;
public abstract class Person {
 public String name;
 public int age;
 public abstract void say() ;
}
package aaaw;
public class Chiness extends Person{
 @Override
 public void say() {
  // TODO Auto-generated method stub
  System.out.println("说中文");
 }
}
package aaaw;
public class American extends Person{
 @Override
 public void say() {
  // TODO Auto-generated method stub
  System.out.println("说英语");
  
 }
}
(二)、雇员示例:
需求:
 公司中程序员有姓名、工号、薪水、工作内容。
 项目经理除了有姓名、工号、薪水、还有奖金、工作内容。
 对给出需求进行数据建模。
分析:
 在这个问题领域中,先找出设计的对象。通过提炼法。
 程序员Programmer:
 属性:姓名name、工号id、薪水sal
 行为:工作work
 经理Manager:
 属性:姓名name、工号id、薪水sal、奖金bouns。
 行为:工作work
序员和经理都属性公司员工,共性资源可以进行抽取。因为他们都是公司的雇员可以将程序员和经理进行抽取,建立体系。
abstract class Employee{
private String name;
private String id;
private double pay;
Employee(String name,String id,double pay){
this.name = name;
this.id = id;
this.pay = pay;
}

public abstract void work();
}


//描述程序员
class Programmer extends Employee{
Programmer(String name,String id,double pay){
super(name,id,pay);
}
public void work(){
System.out.println("code ...");
}
}


//描述经理
class Manager extends Employee{
private int bouns;
Manager(String name,String id,double pay,int bounus){
super(name,id,pay);
this.bouns = bouns;
}

public void work(){
System.out.println("manager");
}
}

public class AbstractTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
class Person{
private String name;
private int age;

Person(String name,int age){
this.name = name;
this.age = age;
}
public String getName(){
return name;
}
public void setname(String name){
this.name = name;
}
}


class Student extends Person{
Student(String name,int age){
super(name,age);
}
}
(三)、抽象类与继承
设计一个能细分为矩形、三角形、圆形和椭圆形的“图形”类。使用继承将这些图形分类,找出能作为基类部分的共同特征(如校准点)和方法(如画法、初始化),并看看这些图形是否能进一步划分为子类。
本题只考虑实现思想,不考虑具体实现的细节,实现方式可自由发挥。
​package com.Oracle.oop5_1;
public abstract class Shape {
 public Shape(Shape s) {
  
 }
 public Shape() {
  
 }
 public abstract double getArea();
}
package com.Oracle.oop5_1;
public class Rectangle extends Shape {
 double width;
 double length;
 
 public Rectangle(double width, double length) {
  super();
  this.width = width;
  this.length = length;
 }
 public double getArea() {
  return width*length;
 }
}
package com.Oracle.oop5_1;
public class Triangle extends Shape {
 double bottom;
 double height;
 public Triangle(double bottom, double height) {
  super();
  this.bottom = bottom;
  this.height = height;
 }
 public double getArea() {
  return 1/2.0*this.bottom*this.height;
 }
}
package com.Oracle.oop5_1;
public class Circle extends Shape {
 double radius;
 public Circle(double radius) {
  this.radius=radius;
 }
 public double getArea() {
  return this.radius*this.radius*Math.PI;
 }
}
package com.Oracle.oop5_1;
import com.Oracle.oop5_1.Circle;
public class Oval extends Circle {
 double radiusShort;
 public Oval(double radius,double radiusShort) {
  super(radius);
  this.radiusShort=radiusShort;
 }
 public double getArea() {
  return 1/2.0*radius*radiusShort;
 }
}
package com.Oracle.oop5_1;
public class Text1 {
 public static void main(String[] args) {
  Shape s1=new Circle(5.2);
  System.out.println(s1.getArea());
  Shape s2=new Rectangle(8.7,9.1);
  System.out.println(s2.getArea());
  Shape s3=new Triangle(4,3);
  System.out.println(s3.getArea());
  Shape s4=new Oval(3,4);
  System.out.println(s4.getArea());
 }
}
(四)、抽象类:
创建一个Vehicle类并将它声明为抽象类。在Vehicle类中声明一个NoOfWheels方法,使它返回一个字符串值。创建两个类Car和Motorbike从Vehicle类继承,并在这两个类中实现NoOfWheels方法。在Car类中,应当显示“四轮车”信息;而在Motorbike类中,应当显示“双轮车”信息。创建另一个带main方法的类,在该类中创建Car和Motorbike的实例,并在控制台中显示消息
package MXDX21;
public class car extends Vehicle {
     public String NoOfWheels(){
         return "四轮车";
     }
}
package MXDX21;
public  class Motorbike extends Vehicle {
    public  String NoOfWheels(){
         return "两轮车";
     }
}
package MXDX21;
public class test {
    public static void main(String[] args) {
        car c=new car();                    //创建car类的实例
        System.out.println(c.NoOfWheels());
        Motorbike m=new Motorbike();        //创建Motorbike类的实例
        System.out.println(m.NoOfWheels());
    }
}
package MXDX21;
 
public abstract class Vehicle {
    //抽象方法NoOfWheels方法返回交通工具类的轮子个数
    abstract String NoOfWheels();
}
五)、抽象类、继承、接口综合【选做】
设计一个系统:
XXX门的实现过程:
流程:
设计一张抽象的门Door,那么对于这张门来说,就应该拥有所有门的共性,开门openDoor()和关门closeDoor();然后对门进行另外的功能设计,防盗--theftproof()、防水--waterproof()、防弹--bulletproof()、防火、防锈……
要求:利用继承、抽象类、接口的知识设计该门
public interface jiben { 
   void opendoor();  
   void closedoor(); 
}  
public class gongneng implements jiben {//创建一个方法类,并连接基本接口
 public void opendoor() {
  // TODO 自动生成的方法存根
System.out.println("开门");
 }
 public void closedoor() {
  // TODO 自动生成的方法存根
  System.out.println("关门");
 }
 public void theftproof(){//加入方法
  System.out.println("防盗");
 }
 public void waterproof(){
  System.out.println("防水");
 }
 public void bulletproof(){
  System.out.println("防弹");
 }
}
public class gongneng implements jiben {//创建一个方法类,并连接基本接口
 public void opendoor() {
  // TODO 自动生成的方法存根
System.out.println("开门");
 }
 public void closedoor() {
  // TODO 自动生成的方法存根
  System.out.println("关门");
 }
 public void theftproof(){//加入方法
  System.out.println("防盗");
 }
 public void waterproof(){
  System.out.println("防水");
 }
 public void bulletproof(){
  System.out.println("防弹");
 }
}
public class gongneng implements jiben {//创建一个方法类,并连接基本接口
 public void opendoor() {
  // TODO 自动生成的方法存根
System.out.println("开门");
 }
 public void closedoor() {
  // TODO 自动生成的方法存根
  System.out.println("关门");
 }
 public void theftproof(){//加入方法
  System.out.println("防盗");
 }
 public void waterproof(){
  System.out.println("防水");
 }
 public void bulletproof(){
  System.out.println("防弹");
 }
}
原文地址:https://www.cnblogs.com/www-hsy-com/p/8119103.html