配套視頻教程
軟件出現(xiàn)的目的
用計(jì)算機(jī)的語(yǔ)言描述現(xiàn)實(shí)世界
用計(jì)算機(jī)解決現(xiàn)實(shí)世界的問題
為什么使用面向?qū)ο?/h3>
世界由對(duì)象組成
面向?qū)ο蟮乃枷?描述 面向?qū)ο蟮氖澜?符合人類思維習(xí)慣
從現(xiàn)實(shí)中抽象出類分三步:
- 找出它的種類
- 找出它的屬性
- 找出它的行為
用面向?qū)ο竺枋鍪澜?/h3>
第一步:發(fā)現(xiàn)類
class Dog {
}
根據(jù)“對(duì)象”抽象出“類”
第二步:發(fā)現(xiàn)類的屬性
狗類共有的特征:
- 品種
- 年齡
- 昵稱
- 健康情況
- 跟主人的親密度
… …
class Dog {
String name = "旺財(cái)"; // 昵稱
int health = 100; // 健康值
int love = 0; // 親密度
String strain = "拉布拉多犬"; // 品種
}
只放和業(yè)務(wù)相關(guān)的屬性
第三步:發(fā)現(xiàn)類的方法
狗類共有的行為:
- 跑
- 吠
- 輸出自己的信息
… …
class Dog {
String name = "旺財(cái)"; // 昵稱
int health = 100; // 健康值
int love = 0; // 親密度
String strain = "拉布拉多犬"; // 品種
/* 輸出狗的信息 */
public void print() {
// 輸出狗信息的代碼
}
}
只放和業(yè)務(wù)相關(guān)的方法
使用類圖描述類
實(shí)踐
實(shí)現(xiàn)領(lǐng)養(yǎng)寵物功能
編寫寵物類Dog和Penguin
創(chuàng)建寵物對(duì)象瞧省,輸入領(lǐng)養(yǎng)的寵物信息并輸出
對(duì)象初始化
Penguin pgn = new Penguin();
pgn.name = "qq";
pgn.sex = "Q仔";
能否在創(chuàng)建對(duì)象的同時(shí)就完成賦值?
使用構(gòu)造方法:
Penguin pgn1 = new Penguin();
class Penguin {
// 屬性
/* 無參構(gòu)造方法 */
public Penguin() {
name = "qq";
love = 20;
sex = "Q仔";
System.out.println("執(zhí)行構(gòu)造方法");
}
}
構(gòu)造方法
系統(tǒng)提供默認(rèn)無參構(gòu)造方法
public Penguin() {
}
自定義構(gòu)造方法
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;
}
系統(tǒng)不再提供默認(rèn)無參構(gòu)造方法
this關(guān)鍵字是對(duì)一個(gè)對(duì)象的默認(rèn)引用斤儿,這里用以區(qū)分同名成員變量
方法重載
System.out.println(45);
System.out.println(true);
System.out.println("狗在玩耍萎河!");
調(diào)用重載方法
pgn = new Penguin();
pgn.print();
pgn = new Penguin("美美", 80, 20, "Q仔");
pgn.print();
一個(gè)例子
class Penguin {
String name = null; //昵稱
int health = 0; // 健康值
String sex = null; // 性別
public void Penguin() {
health=10;
sex="雄";
System.out.println("執(zhí)行構(gòu)造方法");
}
public void print() {
System.out.println("企鵝的名字是" + name + ",健康值是"
+ health + ",性別是" + sex);
}
}
Penguin pgn3= new Penguin();
pgn3.print();
找出下面代碼的問題
class Dog {
private String name = "旺財(cái)"; // 昵稱
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靜態(tài)成員
一個(gè)例子 統(tǒng)計(jì)對(duì)象被創(chuàng)建出來的個(gè)數(shù)
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;
}
// 統(tǒng)計(jì)人數(shù)的函數(shù)
public long getCount(){
return all_count;
}
// 應(yīng)該具備找同齡人的功能
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特點(diǎn)
1 隨著類的加載而加載戳寸,靜態(tài)會(huì)隨著類的加載而加載绰沥,隨著類的消失而消失。說明它的生命周期很長(zhǎng)躯舔。
2 優(yōu)先于對(duì)象存在滞谢。—>靜態(tài)是先存在锁保,對(duì)象是后存在薯酝。
3 被所有實(shí)例(對(duì)象)所共享。
4 可以直接被類名調(diào)用
使用static定義方法
用類名調(diào)用: Person.print();
靜態(tài)方法只能訪問靜態(tài)屬性爽柒,不能訪問實(shí)例屬性
找錯(cuò)誤
class Dog {
private String name = "旺財(cái)"; // 昵稱
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;
屬性隨意訪問吴菠,不合理的賦值
封裝的概念
封裝:將類的某些信息隱藏在類內(nèi)部,不允許外部程序直接訪問浩村,而是通過該類提供的方法來實(shí)現(xiàn)對(duì)隱藏信息的操作和訪問
封裝的好處
1.隱藏類的實(shí)現(xiàn)細(xì)節(jié)
2.只能通過規(guī)定方法訪問數(shù)據(jù)
3.方便加入控制語(yǔ)句
4.方便修改實(shí)現(xiàn)
封裝的步驟
class Dog {
private String name = "旺財(cái)"; // 昵稱
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("健康值應(yīng)該在0和100之間做葵,默認(rèn)值是40");
} else
this.health = health;
}
// 其它getter/setter方法
}
this
用類名定義一個(gè)變量(對(duì)象,實(shí)例)的時(shí)候心墅,定義的只是一個(gè)引用酿矢,外面可以通過這個(gè)引用來訪問這個(gè)類里面的屬性和方法榨乎。
那么類里面是夠也應(yīng)該有一個(gè)引用來訪問自己的屬性和方法呢?
JAVA提供了一個(gè)很好的東西瘫筐,就是 this 對(duì)象蜜暑,它可以在類里面來引用這個(gè)類的屬性和方法。
先來個(gè)簡(jiǎn)單的例子:
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");
}
}
關(guān)于返回類自身的引用严肪,《Thinking in Java》有個(gè)很經(jīng)典的例子史煎。
通過this 這個(gè)關(guān)鍵字返回自身這個(gè)對(duì)象然后在一條語(yǔ)句里面實(shí)現(xiàn)多次的操作
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();
}
}
一個(gè)類中定義兩個(gè)構(gòu)造函數(shù)谦屑,在一個(gè)構(gòu)造函數(shù)中通過 this 這個(gè)引用來調(diào)用另一個(gè)構(gòu)造函數(shù)
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"); //隨便傳進(jìn)去的參數(shù)
tt.print();
}
}
練習(xí)
創(chuàng)建Dog類
編寫Test類
package com.company;
/**
* Created by ttc on 2017/12/28.
*/
//private ,default, protected,public
public class Dog {
private String name = "旺財(cái)"; // 昵稱
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("請(qǐng)輸入寵物名字");
String name = scanner.next();
System.out.println("請(qǐng)輸入寵物類型:1狗,2企鵝");
int type = scanner.nextInt();
if(type == 1)
{
Dog d = new Dog();
System.out.println("請(qǐng)輸入品種,1聰明的拉布拉多氢橙,2酷酷的雪納瑞");
int kind = scanner.nextInt();
d.setKind(kind);
d.setName(name);
System.out.println(d);
}
else
{
//new 企鵝();
}
}
}
創(chuàng)建Penguin類
編寫Test類