zoukankan      html  css  js  c++  java
  • NiXi.DAY06东软实训.:面向对象思想~抽象~static~final~构造方法及其重载

     

    本章技能目标:

    • 使用类图描述设计
    • 掌握面向对象设计的基本步骤
    • 掌握类和对象的概念
    • 掌握构造方法及其重载
    • 掌握封装的概念及其使用

    本章单词:

      class:类

      object:对象

      static:

      final:

      private:

      public:

      protect:

      overloding:方法重载     

      overriding:方法覆盖

      constructor:构造方法

      encapsulation:封装

    1.1用面向对象设计电子宠物系统

      万事万物皆对象

      从具体到抽象

      ¥*¥面向对象的过程就是抽象的过程:分三步来完成

        第一步:发现类

        第二步:发现类的属性

        第三步:发现类的方法

      

     1 package day0702;
     2 /**
     3  * 宠物猫类
     4  * @author 刘晶晶
     5  *
     6  */
     7 public class Cat {
     8     String name="无名氏"; //姓名
     9     String strain = "可爱的Cat"; //品种
    10     int health = 80; //健康值
    11     int love =60;  //与主人的亲密度
    12     
    13     /**
    14      * 输出宠物的信息:自我介绍
    15      */
    16     public void print(){
    17         System.out.println("我的名字是:"+name);
    18         System.out.println("我是:"+strain);
    19         System.out.println("我的健康值是:"+health);
    20         System.out.println("我与主人的亲密度是:"+love);
    21     }
    22 }
    package day0702;
    /**
     * 宠物鱼类
     * @author Administrator
     *
     */
    public class Fish {
        String name = "无名氏"; //姓名
        String sex = "小鱼妹"; //性别
        int health = 60; //健康值
        int love =80;  //与主人的亲密度
        
        /**
         * 输出宠物的信息:自我介绍
         */
        public void print(){
            System.out.println("我的名字是:"+name);
            System.out.println("我是:"+sex);
            System.out.println("我的健康值是:"+health);
            System.out.println("我与主人的亲密度是:"+love);
        }
    }
     1 package day0702;
     2 
     3 import java.util.Scanner;
     4 
     5 /**
     6  * 宠物领养类
     7  * @author Administrator
     8  *
     9  */
    10 public class PetTest {
    11 
    12     /**
    13      * @param args
    14      */
    15     public static void main(String[] args) {
    16         // TODO Auto-generated method stub
    17         System.out.println("欢迎来到宠物之家!");
    18         
    19         //创建键盘录入对象
    20         Scanner input= new Scanner(System.in);
    21         
    22         //主人输入宠物的名字
    23         System.out.println("请输入宠物的名字:");
    24         String petName = input.next();
    25         
    26         //选择宠物的类型
    27         System.out.println("请输入领养宠物的类型:(1:猫   2:鱼)");
    28         
    29         switch (input.nextInt()) {
    30         case 1:
    31             //选择了猫
    32             //选择猫的品种
    33             System.out.println("选择猫的品种:(1:可爱的Cat  2:活泼的Cat)");
    34             String strain=null;
    35             if(input.nextInt()==1){
    36                 strain="可爱的Cat";
    37             }else if(input.nextInt()==2){
    38                 strain="活泼的Cat";
    39             }
    40             //创建猫的对象并赋值
    41             Cat cat=new Cat();
    42             
    43             cat.name=petName;
    44             cat.strain=strain;
    45             
    46             //输出猫的信息   调用print方法
    47             cat.print();
    48             
    49             
    50             break;
    51         case 2:    
    52             //选择了鱼
    53             //选择鱼的性别
    54             System.out.println("请输入鱼的性别:(1:小鱼妹  2:小鱼仔)");
    55             String sex=null;
    56             if(input.nextInt()==1){
    57                 sex ="小鱼妹";
    58             }else if(input.nextInt()==2){
    59                 sex ="小鱼仔";
    60             }
    61             
    62             //创建鱼的对象
    63             Fish fish = new Fish();
    64             fish.name=petName;
    65             fish.sex=sex;
    66             
    67             //输出鱼的信息  调用print()方法
    68             fish.print();
    69             break;
    70 
    71         default:
    72             System.out.println("抱歉,宠物店还没有此类宠物!");
    73             break;
    74         }
    75 
    76     }
    77 
    78 }

    静态变量static final

     

    1.2.2  构造方法及其重载

    无参构造方法

     1 package day0702;
     2 /**
     3  * 宠物猫类
     4  * @author 刘晶晶
     5  *
     6  */
     7 public class Cat {
     8     String name="无名氏"; //姓名
     9     String strain = "可爱的Cat"; //品种
    10     int health = 80; //健康值
    11     int love =60;  //与主人的亲密度
    12     
    13     /**
    14      * 无参构造方法
    15      */
    16     public Cat(){
    17         name="楠楠";
    18         strain="嘻哈的Cat";
    19         health=89;
    20         love=98;
    21         System.out.println("执行构造方法");        
    22     }
    23     
    24     /**
    25      * 输出宠物的信息:自我介绍
    26      */
    27     public void print(){
    28         System.out.println("我的名字是:"+name);
    29         System.out.println("我是:"+strain);
    30         System.out.println("我的健康值是:"+health);
    31         System.out.println("我与主人的亲密度是:"+love);
    32     }
    33     
    34     /**
    35      * 测试无参构造方法的使用
    36      */
    37     public static void main(String[] args){
    38         //创建Cat对象
    39         Cat cat = new Cat();
    40         
    41         //调用cat对象的方法,输出cat的信息
    42         cat.print();
    43     }
    44     
    45 }

    运行结果:

    有参构造方法

     

    1.2.3常见错误

     

     练习

    Dog

    +name:String

    +strain:String

    +health:int

    +love:int

     +print():void

    Dog类图

    上述代码已展示

     1 package day0702;
     2 /**
     3  * 马类    带参构造方法
     4  * @author Administrator
     5  *
     6  */
     7 public class Horse {
     8     /**
     9      * 有参构造方法
    10      * @param name
    11      * @param strain
    12      * @param age
    13      */
    14     public Horse(String name, String strain, int age) {
    15         super();
    16         this.name = name;
    17         this.strain = strain;
    18         this.age = age;
    19     }
    20     /**
    21      * 有参构造方法   name一个参数
    22      */
    23     public Horse(String name){
    24         this.name=name;
    25     }
    26     
    27     /**
    28      * 无参构造方法
    29      */
    30 //    public Horse() {
    31 //        super();
    32 //        // TODO Auto-generated constructor stub
    33 //    }
    34     String name;
    35     String strain;
    36     int age;
    37     
    38     /**
    39      *输出马信息的方法
    40      * @param args
    41      */
    42     public void showHorse(){
    43         System.out.println("马的名字是:"+name);
    44         System.out.println("马的品种是:"+strain);
    45         System.out.println("马的年龄是:"+age);
    46     }
    47     
    48     public static void main(String[] args){
    49         //创建Horse对象
    50         //Horse horse =new Horse();    
    51 //当把前面的无参构造方法注释的时候,此条语句报错,因为缺少无参构造
    52                Horse horse =null;
    53         horse=new Horse("千里良驹");
    54         horse.showHorse();
    55     
    56     }
    57     
    58     
    59 }

     

    ???

     

      

    年轻人能为世界年轻人能为世界做些什么
  • 相关阅读:
    构建自己的外汇智能交易系统
    EA范例
    读书札记:加拿大元因素
    预计欧元近期将“绝境大反攻”
    读书札记:新西兰元因素
    在新的一年里开启新的人生
    旧文重发:行在道上,从局部到全局——与师者高焕堂、赵善中先生谈《大道至简》
    与邹欣先生就《大道至简》一书中的两个主要问题的讨论
    《大道至简》一书第三版,与编辑就本书写作风格的讨论
    与读者们谈谈《大道至简》这五年
  • 原文地址:https://www.cnblogs.com/twinkle-star/p/9253878.html
Copyright © 2011-2022 走看看