关于java:Java零基础系列教程10Java抽象与封装

21次阅读

共计 5444 个字符,预计需要花费 14 分钟才能阅读完成。

配套视频教程

本文 B 站配套视频教程

软件呈现的目标

用计算机的语言形容事实世界
用计算机解决事实世界的问题

为什么应用面向对象

世界由对象组成

面向对象的思维 形容 面向对象的世界 合乎人类思维习惯

从事实中形象出类分三步:

  1. 找出它的品种
  2. 找出它的属性
  3. 找出它的行为

用面向对象形容世界

第一步:发现类
class Dog {

}
依据“对象”形象出“类”

第二步:发现类的属性
狗类共有的特色:

  1. 种类
  2. 年龄
  3. 昵称
  4. 衰弱状况
  5. 跟客人的亲密度

… …

class Dog { 
    String name = "旺财"; // 昵称
    int health = 100; // 衰弱值    
    int love = 0;   // 亲密度
    String strain = "拉布拉多犬"; // 种类 
}

只放和业务相干的属性

第三步:发现类的办法
狗类共有的行为:

  1. 输入本人的信息

… …

class Dog { 
    String name = "旺财";   // 昵称
    int health = 100; // 衰弱值    
    int love = 0;     // 亲密度
    String strain = "拉布拉多犬"; // 种类 
    /* 输入狗的信息 */
    public void print() {// 输入狗信息的代码} 
}

只放和业务相干的办法

应用类图形容类

实际

实现领养宠物性能
编写宠物类 Dog 和 Penguin
创立宠物对象,输出领养的宠物信息并输入

对象初始化

Penguin pgn = new Penguin();
pgn.name = "qq";
pgn.sex = "Q 仔"; 

是否在创建对象的同时就实现赋值?

应用构造方法:
Penguin pgn1 = new Penguin();

class Penguin {
    // 属性
    /* 无参构造方法 */
    public Penguin() {
           name = "qq";
           love = 20;
           sex = "Q 仔";
           System.out.println("执行构造方法");
    }
}

构造方法

零碎提供默认无参构造方法

public Penguin() {}

自定义构造方法

public Penguin () {
        name = "qq";
        love = 20;
        sex = "Q 仔";
}
public Penguin (String name,int health,int love,String sex) {
        this.name = name;
        this.health = health;
        this.love = love;
        this.sex = sex;
}

零碎不再提供默认无参构造方法

this 关键字是对一个对象的默认援用,这里用以辨别同名成员变量

办法重载

System.out.println(45);
System.out.println(true);
System.out.println("狗在游玩!"); 

调用重载办法

pgn = new Penguin();
pgn.print();
pgn = new Penguin("美美", 80, 20, "Q 仔");
pgn.print();

一个例子

class Penguin {
       String name = null; // 昵称
       int health = 0; // 衰弱值
       String sex = null; // 性别
       public void Penguin() {    
               health=10;
               sex="雄";
               System.out.println("执行构造方法");
        }
        public void print() {
               System.out.println("企鹅的名字是" + name + ", 衰弱值是" 
                                                 + health + ", 性别是" + sex);
        }
}
Penguin pgn3= new Penguin();
pgn3.print();

找出上面代码的问题

class Dog {
       private String name = "旺财";   // 昵称
       private int health = 100;  // 衰弱值    
       private int love = 0;     // 亲密度    
       public void play(int n) {
              int localv;
              health = health - n;        
              System.out.println(name+""+ localv +" "+health+" "+love); 
       }
       public static void main(String[] args) {Dog d=new Dog();
              d.play(5);
       }
} 

static 动态成员

一个例子 统计对象被创立进去的个数

class Person
{
    public String name;
    public int age;
    static public long  all_count;
    public Person(){all_count++;}
    public Person(String name , int age){
        all_count++;
        this.name = name;
        this.age = age;
    }
    // 统计人数的函数
    public long getCount(){return all_count;}
    // 应该具备找同龄人的性能
    public boolean isSameAge(Person p1){return this.age == p1.age;}
}
class Demo9 
{public static void main(String[] args) 
    {Person p1 = new Person( "jame" ,  34);
        Person p2 = new Person("lucy" ,  34);

        Person p3 = new Person("lili" ,  34);
        Person p4 = new Person();
        System.out.println(p1.getCount() + "" + p2.getCount() +"  " + p3.getCount()  );
        System.out.println(p1.isSameAge( p2) );
        System.out.println(p1.isSameAge( p3) );
    }
}

4:static 特点
1 随着类的加载而加载,动态会随着类的加载而加载,随着类的隐没而隐没。阐明它的生命周期很长。

      2 优先于对象存在。—> 动态是先存在,对象是后存在。3 被所有实例 (对象) 所共享。4 能够间接被类名调用

应用 static 定义方法

用类名调用:Person.print();

静态方法只能拜访动态属性,不能拜访实例属性

找谬误

class Dog {
       private String name = "旺财"; // 昵称
       private int health = 100;     // 衰弱值
       private int love = 0;        // 亲密度        
       public void play(int n) {
              static int localv=5;        
              health = health - n;        
              System.out.println(name+""+localv+" "+health+" "+love);
       }    
       public static void main(String[] args) {Dog d=new Dog();
              d.play(5);
       }
} 

封装

Dog d = new Dog();
d.health = -1000;
属性随便拜访,不合理的赋值

封装的概念

封装:将类的某些信息暗藏在类外部,不容许内部程序间接拜访,而是通过该类提供的办法来实现对暗藏信息的操作和拜访

封装的益处

1. 暗藏类的实现细节
2. 只能通过规定办法拜访数据
3. 不便退出管制语句
4. 不便批改实现

封装的步骤

class Dog {
    private String name = "旺财"; // 昵称
    private int health = 100; // 衰弱值
    private int love = 0;   // 亲密度
    private String strain = "拉布拉多犬"; // 种类
    public int getHealth() {return health;}
    public void setHealth (int health) {if (health > 100 || health < 0) {
            this.health = 40;
            System.out.println("衰弱值应该在 0 和 100 之间,默认值是 40");
        } else
            this.health  =  health;
    }
    // 其它 getter/setter 办法
}

this

用类名定义一个变量 (对象,实例) 的时候,定义的只是一个援用,里面能够通过这个援用来拜访这个类外面的属性和办法。

那么类外面是够也应该有一个援用来拜访本人的属性和办法呢?

JAVA 提供了一个很好的货色,就是 this 对象,它能够在类外面来援用这个类的属性和办法。
先来个简略的例子:

public class ThisDemo {  
    String name="Mick";
    public void print(String name){System.out.println("类中的属性 name="+this.name);
        System.out.println("部分传参的属性 ="+name);
    }   
    public static void main(String[] args) {ThisDemo tt=new ThisDemo();
        tt.print("Orson");
    }
}

对于返回类本身的援用,《Thinking in Java》有个很经典的例子。
通过 this 这个关键字返回本身这个对象而后在一条语句外面实现屡次的操作

public class ThisDemo {  
    int number;
    ThisDemo increment(){
         number++;
         return this;
    }  
  private void print(){System.out.println("number="+number);
    }
    public static void main(String[] args) {ThisDemo tt=new ThisDemo();
         tt.increment().increment().increment().print();
    }
}

一个类中定义两个构造函数,在一个构造函数中通过 this 这个援用来调用另一个构造函数

public class ThisDemo {  
    String name;
    int age;
    public ThisDemo (){this.age=21;}     
    public ThisDemo(String name){this();
        this.name="Mick";
    }     
  private void print(){System.out.println("最终名字 ="+this.name);
         System.out.println("最终的年龄 ="+this.age);
    }
    public static void main(String[] args) {ThisDemo tt=new ThisDemo("zhangsan"); // 轻易传进去的参数
       tt.print();}
}

练习

创立 Dog 类
编写 Test 类

package com.company;

/**
 * Created by ttc on 2017/12/28.
 */
//private ,default, protected,public
public class Dog {
    private String name = "旺财"; // 昵称
    private int health = 100;     // 衰弱值 0 ---100 private 公有的
    private int love = 0;        // 亲密度
    private int type;// 类型:1 狗 2 企鹅
    private int kind;// 种类

    public String toString()
    {
        String strKind = "";
        if(kind == 1)
        {strKind = "拉布拉多";}
        else if(kind == 2)
        {strKind = "雪纳瑞";}
        String str = "宠物的自白,我的名字叫"
                +name+"衰弱值是"+health+"和客人的亲密度是"+love+"我是一只"+strKind;
        return str;
    }

    public String getName() {return name;}

    public void setName(String name) {this.name = name;}

    public int getHealth() {return health;}

    public void setHealth(int health) {this.health = health;}

    public int getLove() {return love;}

    public void setLove(int love) {this.love = love;}

    public int getType() {return type;}

    public void setType(int type) {this.type = type;}

    public int getKind() {return kind;}

    public void setKind(int kind) {this.kind = kind;}
}

package com.company;

import java.util.Scanner;

public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);
        System.out.println("欢送来到宠物店");
        System.out.println("请输出宠物名字");

        String name = scanner.next();

        System.out.println("请输出宠物类型:1 狗,2 企鹅");
        int type = scanner.nextInt();
        if(type == 1)
        {Dog d = new Dog();
            System.out.println("请输出种类,1 聪慧的拉布拉多,2 酷酷的雪纳瑞");
            int kind = scanner.nextInt();
            d.setKind(kind);
            d.setName(name);
            System.out.println(d);
        }
        else
        {//new 企鹅();
        }

    }
}

创立 Penguin 类
编写 Test 类

正文完
 0