JAVA萌新学习day9
一.面向对象的基本使用
在HeroDemo.java中
import java.util.Random;
public class HeroDemo {
/*
我们以前写代码都在main方法内直接写.
从我们接触面向对象后,尽量少在main方法里写代码.
*/
public static void main(String[] args) {
System.out.println("面向对象的基本使用");
//创建英雄对象,实际就是定义一个英雄类型的变量,并给他赋值
//类类型的变量创建需要使用new关键字,产生的对象在 堆 中保存
Hero hero = new Hero();
//如何调用对象的属性和方法
//使用 . 语法来调用
hero.name = "刘海柱";
hero.age = 28;
hero.occupation = "职业法师";
hero.health = 100;
Random random = new Random();
//英雄刘海柱受到了未知攻击
int number = random.nextInt(50);
int health = hero.attack(number);
//调用显示自己信息的方法
hero.showInfo();
//打印自己当前的生命值
System.out.println(hero.name + "的生命值是:\n" + health);
//转义字符
/*
1.什么叫字符串???
我们管双引号括起来的叫字符串,如果我的字符串内用就是双引号,咋办??
2.因为字符串中有些字符不能直接出现,但一定要出现或有特殊含义时,我们就需要转义.
就是在字符前面加 \
3. \ 是转义字符 如果我想在字符串中输出两个 \\,咋办
*/
String str = " \" \" ";
String str1 = " \\\\ ";
Hero hero2 = new Hero();
//如何调用对象的属性和方法
//使用.语法来调用
hero.name = "MT";
hero.age = 18;
hero.occupation = "职业坦克";
hero.health = 200;
}
}
在Hero.java中
//定义一个英雄类.
//在类的声明大括号内,无论是成员变量声明还是方法声明,我们管这个叫在类的内部使用.
//在类的内部有实际对象存在吗???
public class Hero {
//英雄的名字:字符串类型
String name;
//英雄的年龄,整形
int age;
//英雄的职业,字符串类型
String occupation;
//英雄的生命值,整形
int health;
//介绍自己,把自己基本信息输出
//没有 static 关键字修饰的方法为实例方法(或叫对象方法),只能通过 对象.方法名 方式调用.
public void showInfo(){
System.out.println("我是" + name + ",年龄" + age + ",我的职业" + occupation);
}
//自己受到打击,生命值减少,返回自己当前的生命值.
public int attack(int healthValue){
health = health - healthValue;
return health;
}
}
二.面向对象的练习:学生类的使用
在Stuent.java中
//创建一个学生类,一个学生可以选多门课程
/*
基本类型没有福之前,他的默认值是什么?? int->0 float->0.0.
引用类型没有赋值前,它的默认值是?? 类,数组,字符串都是引用类型,没有赋值前,为null.
因为类的属性和方法,需要使用 对象.属性名 对象.方法名 方式调用.
所以呢,当对象没有赋值时,调用属性或方法,会产生空指针异常.
*/
import java.util.Random;
public class Student {
//姓名
String name;
//学号
int number;
//保存选修的课程名的数组
String[] courseNames;
//保存选修的课程成绩的数组
int[] courseScores;
//构造方法,没有任何返回值(void也不能写),方法名和类名相同
//构造方法是当使用new关键字创建对象的时候,由系统调用
//如果我们不写构造方法,系统会产生一个没有参数的构造方法
//构造方法也能重载
public Student(String name, int number){
this.name = name;
this.number = number;
}
//获得当前学生的总成绩
public int totalScore(){
int sum = 0;
for (int i = 0; i < courseScores.length; i++) {
sum += courseScores[i];
}
//将总成绩返回给调用者
return sum;
}
//比较两个学生的成绩,返回哪个成绩高
//因为当前方法的调用者也是一个学生对象
//所以 此方法就是检查 当前对象自己和传入的对象谁大谁小
public boolean compare(Student otherStudent){
if(otherStudent.totalScore() < this.totalScore() ){
return true;
}
return false;
}
public void printInfo(){
if(courseScores != null && courseNames != null){
if(courseScores.length != courseNames.length) {
System.out.println("课程和成绩不对应");
return;
}
}
System.out.println("姓名:" + name + ",学号:" + number);
for (int i = 0; i < courseNames.length; i++) {
int score = courseScores[i];
System.out.println(courseNames[i] + ":" + score + " ");
}
System.out.println();
}
//用此方法来产生假数据
public void defaultValue(){
//创建课程和成绩数组
courseNames = new String[3];
courseScores = new int[3];
courseNames[0] = "java";
courseNames[1] = "游戏开发";
courseNames[2] = "计算机";
Random random = new Random();
for (int i = 0; i < courseNames.length; i++) {
courseScores[i] = random.nextInt(20) + 80;
}
}
//静态关键字 static
/*
1.当用它来修饰类的方法的时候,如果有这个关键字,这个方法叫类方法,这个方法需要通过 类名.方法名 的方式调用.
没有的话,这个方法叫实例方法(或叫对象方法),这个方法需要通过 对象名.方法名 的方式调用.
2.类中有两类方法:一类叫 类方法 ;另一类叫 实例方法(或对象方法)
3.类方法中不能直接调用实例方法.
4.如果强制用 对象名.方法名 的方式调用类方法也是允许的,产生效果和用类名直接调用.
*/
//学生的测试方法 静态方法(类方法)只能通过 类名.方法名 方式调用.
public static void testStudent(){
Student[] stu = new Student[4];
String[] names = {"刘海柱", "MT", "王可可", "小明"};
for (int i = 0; i < names.length; i++) {
Student student = new Student(names[i],i+1);
//student.name = names[i];
//student.number = i + 1;
//调用学生自己的方法,产生选课信息
student.defaultValue();
//打印自己的选课信息
student.printInfo();
//将当前的学生对象保存到数组中
stu[i] = student;
}
Student a ;
for (int i = 0; i < stu.length-1; i++) {
boolean b = stu[i].compare(stu[i+1]);
if(b == true){
a = stu[i];
System.out.println("成绩最好的学生是:" + names[i] + " " + "成绩是:" + a.totalScore());
break;
}
}
}
}
在StudentDemo.java中
public class StudentDemo {
public static void main(String[] args) {
System.out.println("面向对象的练习:学生类的使用");
//调用学生类测试方法
Student.testStudent();
}
}