JAVA黑马程序员day7(面向对象)
生活随笔
收集整理的這篇文章主要介紹了
JAVA黑马程序员day7(面向对象)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
類和對象的基本概念
封裝
Private權限修飾符
this關鍵字
構造方法:
標準的javaBean類
插件PTG可以一鍵生成javabean標準類
成員變量和局部變量
練習:文字版格斗游戲
Role類
test類
public class test {public static void main(String[] args) {Role role1 = new Role("喬峰", 100);Role role2 = new Role("鳩摩智", 100);while(true){role1.attack(role2);if(role2.getBlood()==0){System.out.println(role1.getName()+"K.O了"+role2.getName());break;}role2.attack(role1);if(role1.getBlood()==0){System.out.println(role2.getName()+"K.O了"+role1.getName());break;}}} }文字版格斗游戲升級版(加招式,性別,樣貌)
Role類
import java.util.Random;public class Role {private String name;private int blood;private char gender;private String face;//長相是隨機的String[] boyface ={"風流俊雅", "氣宇軒昂","面目猙獰"};String[] girlface={"美輪美奐","沉魚落雁","相貌丑陋"};String[] attacks_desc={"%s使出了一招【背心釘】,轉到對方的身后,一掌向%s背心的靈臺穴拍去。","%s使出了一招【游空】,飛起身形子半空中變掌為抓鎖向%s。","%s大喝一聲,一招【劈裂墜地】,錘向%s雙腿",};String[] injureds_desc={"結果%s退了半步,毫發(fā)無傷","結果給%s造成了一處瘀傷",};//空參public Role(){}//全參public Role(String name,int blood,char gender){this.name=name;this.blood=blood;this.gender=gender;setFace(gender);//this.face=face;隨機長相,所以不是在創(chuàng)建對象的時候給的}public char getGender(){return gender;}public void setgender(){this.gender=gender;}public String getFace(){return face;}public void setFace(char gender){Random rd=new Random();if(gender =='男'){int index=rd.nextInt(boyface.length);this.face=boyface[index];}else if(gender=='女'){int index=rd.nextInt(girlface.length);this.face=girlface[index];}else{this.face="面目猙獰";}}public String getName(){return name;}public void setName(String name){this.name=name;}public int getBlood(){return blood;}public void setblood(int blood){this.blood=blood;}public void attack(Role role){//方法的調用者去攻擊參數(shù),參數(shù)是對象,而不是一個name了Random r=new Random();int index2=r.nextInt(attacks_desc.length);String kunfu=attacks_desc[index2];//輸出一個攻擊效果System.out.printf(kunfu,this.getName(),role.getName());//souf沒有換行效果System.out.println();int hurt=r.nextInt(20);//剩余血量int remainblood=role.getBlood()-hurt;remainblood=remainblood<0?0:remainblood;//剩余血量如果為負數(shù)了,就修改為0role.setblood(remainblood);//這里要修改一下,被揍的人的血量,其血量為當前所剩的血量//受傷效果的描述//血量>90 0索引描述if(remainblood>90){System.out.printf(injureds_desc[0],role.getName());//此處要有souf,其中%s表示占位,后面的參數(shù)表示填充進去}else{System.out.printf(injureds_desc[1],role.getName());}System.out.println();//System.out.println(this.name+"舉起拳頭打了"+role.name+"一下,造成了"+hurt+"點傷害,"+role.getName()+"還剩下"+remainblood+"點血");}public void showRoleinfo(){System.out.println("姓名為:"+getName());System.out.println("血量為:"+getBlood());System.out.println("性別為:"+getGender());System.out.println("長相為:"+getFace());}}test類
public class test {public static void main(String[] args) {Role role1 = new Role("喬峰", 100, '男');Role role2 = new Role("鳩摩智", 100, '男');role1.showRoleinfo();role2.showRoleinfo();while(true){role1.attack(role2);if(role2.getBlood()==0){System.out.println(role1.getName()+"K.O了"+role2.getName());break;}role2.attack(role1);if(role1.getBlood()==0){System.out.println(role2.getName()+"K.O了"+role1.getName());break;}}} }練習:對象數(shù)組1
Goods類
test類
public class test {public static void main(String[] args) {//創(chuàng)建一個對象數(shù)組Goods[] arr=new Goods[3];//創(chuàng)建三個商品對象Goods g1=new Goods("001","華為p40",2000,100);Goods g2=new Goods("002","保溫杯",100,50);Goods g3=new Goods("003","鼠標",200,20);//把商品添加到數(shù)組中arr[0]=g1;arr[1]=g2;arr[2]=g3;//遍歷for (int i = 0; i < arr.length; i++) {Goods goods=arr[i];//創(chuàng)建一個新對象用來接收對象數(shù)組中的對象System.out.println(goods.getId()+","+goods.getName()+","+goods.getPrice()+","+goods.getPrice());}} }鍵盤錄入
第一套體系:
nextInt();接收整數(shù)
nextDouble();接收小數(shù)
next();接收字符串
//第一套體系的特點:遇到空格,回車就停止接收,這些符號后面的數(shù)據(jù)就不會被接收了
第二套體系:
nextLine();接收字符串
//可以接收空格,制表符,遇到回車才停止接收數(shù)據(jù)
//注意兩套鍵盤錄入體系最好不要混用,先以第一套體系為主
練習:對象數(shù)組2
Car類
test類
import java.util.Scanner;public class test {public static void main(String[] args) {Car[] arr=new Car[3];Scanner sc=new Scanner(System.in);for (int i = 0; i < arr.length; i++) {Car c=new Car();System.out.println("請輸入汽車品牌");String brand=sc.next();c.setBrand(brand);System.out.println("請輸入汽車的價格");int price=sc.nextInt();c.setPrice(price);System.out.println("請輸入汽車的顏色");String color=sc.next();c.setColor(color);arr[i]=c;}for (int i = 0; i < arr.length; i++) {System.out.println(arr[i].getBrand()+","+arr[i].getPrice()+","+arr[i].getColor());}} }練習:對象數(shù)組3(求平均價格)
Phone類
test類
public class test {public static void main(String[] args) {Phone[] arr=new Phone[3];//定義對象數(shù)組//創(chuàng)建手機對象Phone phone1=new Phone("小米",1999,"粉色");//實例化對象Phone phone2=new Phone("華為",4999,"白色");Phone phone3=new Phone("魅族",3999,"黑色");int sum=0;double average=0.0;//把手機對象添加到數(shù)組中arr[0]=phone1;arr[1]=phone2;arr[2]=phone3;for (int i = 0; i < arr.length; i++) {sum+=arr[i].getPrice();}average=sum*1.0/arr.length;System.out.println(average);} }練習:對象數(shù)組4(定義女朋友對象)
GirlFriend類
test類
package day2;public class test {public static void main(String[] args) {GirlFriend[] arr=new GirlFriend[4];GirlFriend g1=new GirlFriend("小芳",29,'女',"唱歌");GirlFriend g2=new GirlFriend("小菜",26,'女',"hipop");GirlFriend g3=new GirlFriend("圓圓",18,'女',"跳舞");GirlFriend g4=new GirlFriend("方方",23,'女',"踢足球");arr[0]=g1;arr[1]=g2;arr[2]=g3;arr[3]=g4;int sum=0;int average=0;for (int i = 0; i < arr.length; i++) {sum+=arr[i].getAge();}average=sum/arr.length;System.out.println("女朋友的平均年齡是"+average);int count=0;for (int i = 0; i < arr.length; i++) {if(arr[i].getAge()<average){arr[i].showInfo();count++;}}System.out.println("比平均年齡小的共有"+count+"個");}}練習:對象數(shù)組5(學生信息管理)
Student類
Stutest類
package day2;import java.util.Scanner;public class Stutest {public static void main(String[] args) {Student[] arr=new Student[3];Student stu1=new Student("張三",202316,18);Student stu2=new Student("李四",202317,19);Student stu3=new Student("王五",202318,20);//將對象添加到數(shù)組當中arr[0]=stu1;arr[1]=stu2;arr[2]=stu3;//要添加的對象Student stu4=new Student("趙六",202319,35);//添加一個學生對象//數(shù)組已滿,需要創(chuàng)建新的數(shù)組,將老數(shù)組的元素添加到新數(shù)組中,數(shù)組長度為老數(shù)組+1//添加的時候要判斷學號的唯一性Student[] arr2=new Student[arr.length+1];if(!Repeat(arr,stu4)){if(getCount(arr)<arr.length){//數(shù)組中元素個數(shù)小于數(shù)組長度說明還有空位置arr[getCount(arr)]=stu4;Check(arr);}else{arr2=newArr(arr,stu4);Check(arr2);}}else{System.out.println("Id重復,請修改id后重新添加");}//通過id刪除學生信息Scanner sc=new Scanner(System.in);System.out.println();System.out.println("請輸入你想要學生年齡+1的ID");int num=sc.nextInt();if(index(num,arr2)>0){arr2[index(num,arr2)].setAge(arr2[index(num,arr2)].getAge()+1);}else{System.out.println("沒有找到");}Check(arr2);}//定義方法:按照指定id返回其數(shù)組下標public static int index(int idnum,Student[] Arr){for (int i = 0; i < Arr.length; i++) {if(Arr[i].getId()==idnum){return i;}}return -1;}//要判斷遍歷老數(shù)組還是新數(shù)組,代碼會重復,所以定義一個方法遍歷所有學生信息public static void Check(Student[] Arr){for (int i = 0; i < Arr.length; i++) {if(Arr[i]!=null){//不為空才打印System.out.println(Arr[i].getName()+","+Arr[i].getId()+","+Arr[i].getAge());}}}//定義方法:創(chuàng)建新的數(shù)組,并將老數(shù)組中的元素加到新數(shù)組中public static Student[] newArr(Student[] Arr,Student Stu4){Student[] Arr2=new Student[Arr.length+1];for (int i = 0; i < Arr.length; i++) {//將老數(shù)組的元素添加到新數(shù)組中Arr2[i]=Arr[i];}Arr2[Arr.length]=Stu4;return Arr2;}//定義方法獲得數(shù)組中元素的個數(shù)public static int getCount(Student[] arr){int count=0;for (int i = 0; i < arr.length; i++) {if(arr[i]!=null){count++;}}return count;}//判斷加的元素id是否重復public static Boolean Repeat(Student[] arr,Student Stu4){for (int i = 0; i < arr.length; i++) {//判斷id是否重復,不重復則添加if(arr[i]!=null) {//此處要加非空判斷if (arr[i].getId() == Stu4.getId()) {return true;}}}return false;} }總結
以上是生活随笔為你收集整理的JAVA黑马程序员day7(面向对象)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言中的变量底部下划线表示什么,编程中
- 下一篇: 面向对象综合训练综合练习