zoukankan      html  css  js  c++  java
  • JAVA进阶2

     间歇性混吃等死,持续性踌躇满志系列-------------第2天

    1、父类子类继承(注:一个JAVA源文件中只能有一个public类,public 类的名字必须和这个编译单元的文件名完全相同,包括大小
    写。)

     1 public class Bird{
     2     String color ="red";      //颜色
     3     String skin = "羽毛";     //皮毛
     4         }
     5 
     6 class Pigeon extends  Bird{
     7     public static void main(String[] args){
     8         Pigeon pigeon = new Pigeon();
     9         System.out.println(pigeon.color);
    10         System.out.println(pigeon.skin);
    11     }
    12         }
    View Code

    运行结果图

    2、经理和员工的差异(子类与父类案例)

     1 import java.util.Date;      //导入java.util.Date类
     2 class Employee{
     3     private String name;        //员工的姓名
     4     private double salary;      //员工的工资
     5     private Date birthday;      //员工的生日
     6 
     7     public String getName(){    //获取员工的姓名
     8         return name;
     9     }
    10     public void setName(String name){       //设置员工姓名
    11         this.name = name;
    12     }
    13     public double getSalary(){      //获取员工的工资
    14         return salary;
    15     }
    16     public void setSalary(double salary){   //设置员工工资
    17        this.salary = salary;
    18     }
    19     public Date getBirthday(){      //获取员工生日
    20         return birthday;
    21     }
    22     public void setBirthday(Date birthday){     //设置员工生日
    23         this.birthday = birthday;
    24     }
    25 }
    26 class Manager extends Employee{
    27     private double bonus;       //经理的奖金
    28     public double getBonus(){   //获取经理的奖金
    29         return bonus;
    30     }
    31     public void setBonus(double bonus){     //设置奖金
    32         this.bonus = bonus;
    33     }
    34 }
    35 public class Test{
    36     public static void main(String[] args){
    37         Employee employee = new Employee();     //创建Employee对象并为其赋值
    38         employee.setName("c#");
    39         employee.setSalary(99);
    40         employee.setBirthday(new Date());
    41         Manager manager = new Manager();        //创建Manager对象并为其赋值
    42         manager.setName("hello");
    43         manager.setSalary(300);
    44         manager.setBirthday(new Date());
    45         manager.setBonus(200);
    46         System.out.println("员工的姓名:"+employee.getName());
    47         System.out.println("员工的工资:"+employee.getSalary());
    48         System.out.println("员工的生日:"+employee.getBirthday());
    49         //输出经理和员工的属性值
    50         System.out.println("经理的姓名:"+manager.getName());
    51         System.out.println("经理的工资:"+manager.getSalary());
    52         System.out.println("经理的生日:"+manager.getBirthday());
    53         System.out.println("经理的奖金:"+manager.getBonus());
    54     }
    55 }
    View Code

     运行结果图

    3、多态(方法的重载,重载的方法之间并不一定必须有联系,但为了提高程序可读性,一般只重载功能相似的方法。)

    注:在进行方法重载时,方法返回值的类型不能作为区分方法的标志。

     1 public class Calculate{
     2     final float PI = 3.14159f;   //定义一个用于笔试圆周率的常量PI
     3     //求圆的面积
     4     public float getArea(float r){   //定义一个用于计算面积的方法getArea()
     5         float area = PI*r*r;
     6         return area;
     7     }
     8     //求矩形的面积
     9     public float getArea(float l,float w){
    10         float area = l*w;       //重载getArea()方法
    11         return area;
    12     }
    13     //画任意形状的图形
    14     public void draw(int num){      //定义一个用于画图的方法draw()
    15         System.out.println("画"+num+"个任意形状的图形");
    16     }
    17     public void draw(String shape){   //重载draw()方法
    18         System.out.println("画一个"+shape);
    19     }
    20 public static void main(String[] args){
    21         Calculate calculate = new Calculate();      //创建Calculate类的对象并为其分配内存
    22         float l = 20;
    23         float w = 40;
    24         float areaRectangle = calculate.getArea(l,w);
    25         System.out.println("求长为"+l+"宽为"+w+"的矩形的面积是:"+areaRectangle);
    26         float r = 7;
    27         float areaCirc = calculate.getArea(r);
    28         System.out.println("求半径为"+r+"的圆的面积是:"+areaCirc);
    29         int num = 10;
    30         calculate.draw(num);
    31         calculate.draw("三角形");
    32 }
    33 }
    View Code

     运行结果图

    4、计算几何图形的面积(使用抽象方法计算面积)

     1 abstract class Shape{
     2     public String getName(){        //获得图形的名称
     3         return this.getClass().getName();
     4     }
     5     public abstract double getArea();     //获得图形的面积
     6 }
     7 class Circle extends Shape{
     8     private double radius;
     9     public Circle(double radius){       //获得圆形的半径
    10         this.radius = radius;
    11     }
    12     public double getArea(){        //计算圆形的面积
    13         return Math.PI*Math.pow(radius,2);
    14     }
    15 }
    16 class  Rectangle extends Shape{
    17     private double length;
    18     private double width;
    19     public Rectangle(double length,double width){
    20         //获得矩形的长度和宽
    21         this.length = length;
    22         this.width = width;
    23     }
    24     public double getArea(){
    25         return length*width;
    26     }
    27 }
    28 public class Test{
    29     public static void main(String[] args){
    30         //创建圆形的对象并将半径设置成10
    31         Circle circle = new Circle(10);
    32         System.out.println("图形的名称是:"+circle.getName());
    33         System.out.println("图形的面积是:"+circle.getArea());
    34         //创建矩形的对象并将长和宽设置成2
    35         Rectangle rectangle = new Rectangle(2,2);
    36         System.out.println("图形的名称是:"+rectangle.getName());
    37         System.out.println("图形的面积是:"+rectangle.getArea());
    38     }
    39 }
    View Code

     运行结果图

    5、简单的汽车销售市场

     1 abstract class Car{
     2     //用来描述汽车的信息
     3     public abstract String getInfo();
     4 }
     5 class BMW extends Car{
     6     //用来描述汽车的信息
     7     public String getInfo(){
     8         return "BMW";
     9     }
    10 }
    11 class Benz extends Car{
    12     //用来描述汽车的信息
    13     public String getInfo(){
    14         return "Benz";
    15     }
    16 }
    17 class CarFactory{
    18     public static Car getCar(String name){
    19         //如果需要BMW则创建BMW对象
    20         if(name.equalsIgnoreCase("BMW")){
    21             return new BMW();
    22         }else if(name.equalsIgnoreCase("Benz")){    //如果需要Benz则创建Benz对象
    23             return new Benz();
    24         }else{
    25             //暂时不支持其他车型
    26             return null;
    27         }
    28     }
    29 }
    30 public class Customer{
    31     public static void main(String[] args){
    32         System.out.println("顾客要购买BMW");
    33         //顾客要购买BMW
    34         Car bmw = CarFactory.getCar("BMW");
    35         //提取BMW
    36         System.out.println("提取汽车:"+bmw.getInfo());
    37         System.out.println("顾客要购买benz");
    38         //顾客要购买BMW
    39         Car benz = CarFactory.getCar("benz");
    40         //提取BMW
    41         System.out.println("提取汽车:"+benz.getInfo());
    42     }
    43 }
    View Code

     运行结果图

    6、抽象类(抽象方法不能使用private或static关键字进行修饰)

     1 abstract class Fruit{       //定义抽象类
     2     //定义颜色成员变量
     3     public String color;
     4     //定义构造方法
     5     public Fruit(){
     6         color = "red";
     7     }
     8     //定义抽象方法
     9     public abstract void harvest();
    10 }
    11 class Apple extends Fruit{
    12     public void harvest(){
    13         //输出字符串:"苹果已经收获!"
    14         System.out.println("苹果已经收获!");
    15     }
    16 }
    17 class Orange extends Fruit{
    18     public void harvest(){
    19         //输出字符串:"桔子已经收获!"
    20         System.out.println("桔子已经收获");
    21     }
    22 }
    23 public class Farm{
    24     public static void main(String[] args){
    25         System.out.println("调用Apple类的harvest()方法的结果:");
    26         //声明Apple类的一个对象apple,并为其分配内存
    27         Apple apple = new Apple();
    28         apple.harvest();
    29         System.out.println("调用Orange类的harvest()方法的结果:");
    30         //声明Orangle类的一个对象orangle,并为其分配内存
    31         Orange orange = new Orange();
    32         orange.harvest();
    33     }
    34 }
    View Code

     运行结果图

     

  • 相关阅读:
    RESTful API
    访问方式由http改为https curl:(51)
    java.lang.OutOfMemoryError: PermGen space
    liunx下tomcat启动 Cannot find ./catalina.sh
    Java-编译后出现$1.class、$2.class等多个class文件
    错误处理的返回--异常还是返回值
    ubuntu 上安装温度检测
    mysql5.6不能输入中文
    jmap在ubuntu上DebuggerException: Can't attach to the process
    tomcat-reload-与内存泄露
  • 原文地址:https://www.cnblogs.com/Anemia-BOY/p/10524345.html
Copyright © 2011-2022 走看看