目录

Educoder头歌JAVA实训JAVA面向对象类与对象

Educoder/头歌JAVA实训——JAVA面向对象:类与对象

第1关:什么是类,如何创建类

什么是类

类: 类是一个模板,它描述一类对象的 行为属性 。 对象:对象是类的一个实例,有 属性行为

举个例子:

人是一个 “类”,小明就是人的 “对象” ,女生/男生是一个类,你的女朋友/男朋友就是一个对象,这个对象的属性有:名字,性别,年龄;行为有:吃饭、睡觉、学习等。

在Java中对象的状态就是属性,行为通过方法体现,即Java中的对象一般由属性和行为组成。

怎么定义类

需要使用 class ,声明这个类的名字。 举例说明:

  1. class Student{
  2. //声明这个是一个学生类
  3. }

学生类中有年龄,姓名,性别属性,方法为吃饭,睡觉和学习。 https://i-blog.csdnimg.cn/blog_migrate/f1b7e7f259d308d6ae170b579b91af04.png

https://i-blog.csdnimg.cn/blog_migrate/917be7ae7e2f838f2c509574643136bf.png

https://i-blog.csdnimg.cn/blog_migrate/ef192d49ea5f711183fd7d6cc72dacd7.png https://i-blog.csdnimg.cn/blog_migrate/0d5fe3641e46e8e92cf03d073c81ea6a.png

package step1;

public class Test {
public static void main(String[] args) {

    	/********** Begin **********/
    	//创建Dog对象
        Dog wuhuarou=new Dog();
    	//设置Dog对象的属性
    	wuhuarou.name="五花肉";
        wuhuarou.color="棕色";
        wuhuarou.variety="阿拉斯加";

    	//输出小狗的属性
    	System.out.println("名字:" + wuhuarou.name + ",毛色:" +wuhuarou.color + ",品种:" +wuhuarou.variety);

    	//调用方法
        wuhuarou.eat();
        wuhuarou.run();

    	/********** End **********/
    }

}

//在这里定义 Dog 类

/********** Begin **********/
class Dog{
String name;
String color;
String variety;
void eat(){
System.out.println("啃骨头");

    }
    void run(){
        System.out.println("叼着骨头跑");
    }

}
/********** End **********/

第 2 关:构造方法

相关知识

为了完成本关任务,你需要掌握:1.什么是构造方法,2.如何定义和调用构造方法。

什么是构造方法

构造方法: 对象被创建的时候会调用的方法 ,对象在被创建的时候,也就是被 new 的时候,会 自动调用构造方法

举个例子:

https://i-blog.csdnimg.cn/blog_migrate/bbfe9aebe34abc4d8a0e64ee319d6211.png

输出:

我被调用了

怎么定义和使用构造方法

怎么定义构造方法呢?构造方法和我们之前学习的方法又有啥区别呢? 请看图:

https://i-blog.csdnimg.cn/blog_migrate/be878ecad04abb143f45e3d0ff6f8c6a.png

看出区别来了吗? 好,我来总结一下,看你是否能在上图中找出相对应的代码:

接下来我在 main 方法中创建 Student 对象代码如下:

  1. public static void main(String[] args){
  2. Student stu = new Student();
  3. Student stu1 = new Student("张三");
  4. }

你觉得会有输出吗,如果有那么输出结果会是什么呢?如果没有那你觉得原因是什么呢?

https://i-blog.csdnimg.cn/blog_migrate/a38d9f3fceba24358fa503343abb0838.png

package step2;

import java.util.Scanner;

public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name = sc.next();
String sex = sc.next();
/********** Begin **********/
//分别使用两种构造器来创建 Person 对象  
 Person peo=new Person();
Person p=new Person(name,sex);

    	/********** End **********/
    }

}

//创建 Person 对象,并创建两种构造方法
/********** Begin **********/
class Person{
String name;
String sex;
public Person(){
System.out.println("一个人被创建了");
}
public Person(String a,String b){
name=a;
sex=b;
System.out.println("姓名:"+name+",性别:"+sex+",被创建了");
}
}

/********** End **********/

第 3 关:选择题(一)

https://i-blog.csdnimg.cn/blog_migrate/e852cd641a130a3145c82c423667ff60.png

https://i-blog.csdnimg.cn/blog_migrate/72bac7c298687d37a1fe47763ba0eba5.png

https://i-blog.csdnimg.cn/blog_migrate/e47977e6b78f29260428a2f052ba8dd1.png

第 4 关:This 关键字

https://i-blog.csdnimg.cn/blog_migrate/35db2ddb0c29bde7906086c86675f982.png

https://i-blog.csdnimg.cn/blog_migrate/88c6cc2bb6dbad0d30c2d272793daf54.png

https://i-blog.csdnimg.cn/blog_migrate/4ab647f5bfa4e1c59ac932d4bd20c2d6.png https://i-blog.csdnimg.cn/blog_migrate/50918a8990b91fc64511d83d4e0b6ca0.png

https://i-blog.csdnimg.cn/blog_migrate/2160f09862f664c922ec2974c1d87c9e.png https://i-blog.csdnimg.cn/blog_migrate/5b5668159cbe0b3fb0c86a6ec5c7ec26.png

package step3;

import java.util.Scanner;

public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name = sc.next();
int age = sc.nextInt();
String sex = sc.next();
Person p = new Person(name,age,sex);
p.display();
}
}

class Person{
String name = "张三";
int age = 18;
String sex = "男";
/********** Begin **********/

    public Person(String name,int age,String sex){
    	this(age);
    	this.name = name;
    	this.sex = sex;
    }

    public Person(int age){
    	this.age = age;
    }

    public void display(){
    	String name = "李四";
    	int age = 11;
    	String sex = "男";
    	System.out.println("name:" + this.name);
    	System.out.println("age:" + this.age);
    	System.out.println("sex:" + this.sex);
    }
    /********** End **********/

}

第 5 关:类与对象练习

https://i-blog.csdnimg.cn/blog_migrate/f6367d1cddb33ec68041d301cc24e799.png

https://i-blog.csdnimg.cn/blog_migrate/5c0f5f671a8fe8a6f6212f4eabb5f7aa.png

https://i-blog.csdnimg.cn/blog_migrate/3fd0368bc2566f88929e374f50c65a16.png

https://i-blog.csdnimg.cn/blog_migrate/cde26e820bce366c893e97a7cf71b854.png

// Test.java

package step4;

import java.util.Scanner;
import step4.WuMingFen;

public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String theMa = sc.next();
int quantity = sc.nextInt();
boolean likeSoup = sc.nextBoolean();
/********** Begin **********/
//使用三个参数的构造方法创建 WuMingFen 对象 取名 f1
WuMingFen f1=new WuMingFen(theMa,quantity,likeSoup);

    	//使用两个参数的构造方法创建WuMingFen对象  取名 f2
        WuMingFen f2=new WuMingFen(theMa,quantity);

    	//使用无参构造方法创建WuMingFen对象  取名 f3
        WuMingFen f3=new WuMingFen();

    	//分别调用三个类的 check方法
        f1.check();
        f2.check();
        f3.check();
    	/********** End **********/
    }

}
// WuMingFen.java

/********** Begin **********/
//在这里添加包名 step4
package step4;
//创建类 添加属性和方法
public class WuMingFen{
String theMa;
int quanlity;
boolean likeSoup;

    public WuMingFen(){
        this.theMa="酸辣";
        this.quanlity=2;
        this.likeSoup=true;
    }
    public WuMingFen(String a,int b){
        this.theMa=a;
        this.quanlity=b;
        this.likeSoup=false;
    }
    public WuMingFen(String a,int b,boolean c){
        this.theMa=a;
        this.quanlity=b;
        this.likeSoup=c;
    }
    void check(){
        System.out.println("面码:"+theMa+",粉的份量:"+quanlity+"两,是否带汤:"+likeSoup);
    }

}

/********** End **********/ 

第 6 关:static 关键字

什么是 static 关键字

static 关键字我们经常接触,不过我们一直没有讨论过它到底是什么,有什么具体的作用,那 static 关键字是什么呢,有啥用呢?

static静态的 意思,是一个修饰符,就像是一个形容词,是用来形容类,变量,方法的。

static 修饰变量,这个变量就变成了静态变量,修饰方法这个方法就成了静态方法,

static 关键字方便在没有创建对象的情况下来进行调用(方法/变量)。

static 关键字的作用

static 关键字你可以理解为是一个形容词,一般是用来形容类、方法、变量,代码块,还有一个作用是用来静态导包,本关我们只讨论它的三个用法。

1.修饰变量 不使用 static 关键字访问对象的属性:

https://i-blog.csdnimg.cn/blog_migrate/5b5937899febe4088f749c17a80078ae.png

https://i-blog.csdnimg.cn/blog_migrate/e8acf600db943fb7138b5f59c5767eaf.png

注意: 如果一个类的成员变量被 static 修饰了,那么所有该类的对象都共享这个变量。无论这个类实例化多少对象,它的静态变量只有一份拷贝  。

https://i-blog.csdnimg.cn/blog_migrate/956c9fea20e77d0f70db7e400b37bec1.png

输出:

李四

李四

李四

2.修饰方法

static 关键字修饰的方法叫做静态方法。静态方法我们已经用过,它有一个特点相信你已经很熟悉,那就是 不需要创建对象 就可以 直接使用 。 如:

https://i-blog.csdnimg.cn/blog_migrate/a9281a57468092e92dd08032d95c46b7.png

注意:

https://i-blog.csdnimg.cn/blog_migrate/f1a98f14eadd9d258d297d7caef4a7ed.png https://i-blog.csdnimg.cn/blog_migrate/d09de241a23ea09a33f8c7786ff03365.png

https://i-blog.csdnimg.cn/blog_migrate/34635449f1659a69f64a5558e5ac0cbf.png

上图中 static{ } 就是一个静态代码块。

我们在 main 方法中没有编写任何代码,可是运行的时候,程序还是会输出 我被调用了 ,由此我们可以发现 静态代码块是不需要依赖 main 方法就可以独立运行 的。

关于静态代码块你只需要记住一句话:在 类被加载的时候 运行且 只运行一次

静态代码块中变量和方法的调用也遵守我们之前所说的规则,即只能直接调用静态的属性和方法。

https://i-blog.csdnimg.cn/blog_migrate/e0155e6919de7fa7282a2796d12468ec.png

package step5;

public class Test {
/********** Begin **********/
static String name = "楚留香";
public static void main(String[] args) {
System.out.println("hello educoder");
System.out.println("我叫" + name);
study();
}
public static void study(){
System.out.println("我喜欢在 educoder 上学习 java");
}
/********** End **********/
}

第 7 关:选择题(二)

https://i-blog.csdnimg.cn/blog_migrate/45a57abaa15c89290451dfd681520e58.png https://i-blog.csdnimg.cn/blog_migrate/21d70d1ece4be33bb8368afab3bf36dc.png https://i-blog.csdnimg.cn/blog_migrate/2923bcd63ee22160e901184aa678ac80.png