【达内课程】面向对象简介

本文介绍了Java面向对象的基本概念,包括类、对象、成员变量及方法,并通过Soldier、FlashLight和Car三个类的具体示例,详细讲解了如何定义类、创建对象及调用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概念了解

Java是一种面向对象的程序设计语言,了解面向对象的编程思想对于学习Java开发相当重要。

面向对象是一种符合人类思维习惯的编程思想。现实生活中存在各种形态不同的事物,这些事物之间存在着各种各样的联系。在程序中使用对象来映射现实中的事物,使用对象的关系来描述事物之间的联系,这种思想就是面向对象。

面向对象和面向过程的区别
提到面向对象,自然会想到面向过程,面向过程就是分析解决问题所需要的步骤,然后用函数把这些步骤一一实现,使用的时候一个一个依次调用就可以了。面向对象则是把解决的问题按照一定规则划分为多个独立的对象,然后通过调用对象的方法来解决问题。当然,一个应用程序会包含多个对象,通过多个对象的相互配合来实现应用程序的功能,这样当应用程序功能发生变动时,只需要修改个别的对象就可以了,从而使代码更容易得到维护。

首先了解几个概念:


类是一个模板,它描述一类对象的行为和状态。

对象(实例)
对象是类的一个实例(对象不是找个女朋友),有状态和行为。例如,一条狗是一个对象,它的状态有:颜色、名字、品种;行为有:摇尾巴、叫、吃等。
每个对象占用独立的内存空间,保存各自的属性数据。
每个对象可以独立控制,让它执行指定的方法代码。

举例说明:创建一个Soldier类

我们定义一个士兵类,他的属性有 id(表示他的唯一编号)、blood(表示他的血量)。他的行为有 go(前进)、attack(攻击)

Soldier类

public class Soldier {
    //成员变量
    int id;//唯一编号,默认0
    int blood = 100;//血量,默认满血


    //成员方法
    public void go(TextView tv) {
        if (blood == 0) {
            tv.setText(id + "已阵亡,无法前进" + "\n" + tv.getText());
            return;
        }
        tv.setText(id + "前进" + "\n" + tv.getText());
    }

    public void attack(TextView tv) {
        if (blood == 0) {
            tv.setText(id + "已阵亡,无法攻击" + "\n" + tv.getText());
            return;
        }

        tv.setText(+id + "号士兵发起进攻" + "]\n" + tv.getText());

        int b = new Random().nextInt(30);
        if (blood < b) {
            blood = b;
        }
        blood -= b;

        if (blood == 0) {
            tv.setText("["+id + "号士兵阵亡" + "\n" + tv.getText());
        } else {
            tv.setText("[士兵" + id + "进攻完毕,血量" + blood + "\n" + tv.getText());
        }
    }
}

xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:text="新建士兵" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:text="前进" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:text="进攻" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#222222"
        android:textSize="15sp"/>
</LinearLayout>

MainActivity

public class MainActivity extends AppCompatActivity {
    TextView textView;
    Soldier s1;//默认null

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textView);
    }

    public void doClick(View view) {
        switch (view.getId()) {
            case R.id.button1:
                f1();
                break;
            case R.id.button2:
                f2();
                break;
            case R.id.button3:
                f3();
                break;
        }
    }

    public void f1() {
        s1 = new Soldier();
        s1.id = 9527;
        //用s1找到士兵对象内存空间
        //访问它的属性变量id
        textView.setText("士兵9527已创建" + "\n");
    }

    public void f2() {
        s1.go(textView);
    }

    public void f3() {
        s1.attack(textView);
    }
}

运行程序:
在这里插入图片描述

举例说明:创建一个FlashLight类

我们来创建一个手电筒的类。它的属性有颜色、开光状态。它的方法有开灯、关灯

FlashLight.java

public class FlashLight {
    //属性变量,成员变量
    int color = Color.BLACK;
    boolean on = false;

    public void turnOn() {
        on = true;
    }

    public void turnOff() {
        on = false;
    }
}

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/black"
    android:orientation="vertical">

    <ToggleButton
        android:id="@+id/toggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:textOff=""
        android:textOn="" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:text="" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:text="" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:text="" />

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
    FlashLight flashLight = new FlashLight();
    LinearLayout linearLayout;
    ToggleButton toggleButton;

    Button red;
    Button white;
    Button blue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        linearLayout = findViewById(R.id.layout);
        toggleButton = findViewById(R.id.toggleButton);
        red = findViewById(R.id.button1);
        white = findViewById(R.id.button2);
        blue = findViewById(R.id.button3);
    }

    public void doClick(View view) {
        switch (view.getId()) {
            case R.id.button1:
                f1();
                break;
            case R.id.button2:
                f2();
                break;
            case R.id.button3:
                f3();
                break;
            case R.id.toggleButton:
                f4();
                break;
        }
    }

    public void f1() {
        //用light变量找到手电筒对象的内存空间地址
        //访问它的color变量
        flashLight.color = Color.RED;
        show();
    }

    public void f2() {
        flashLight.color = Color.WHITE;
        show();
    }

    public void f3() {
        flashLight.color = Color.BLUE;
        show();
    }

    public void f4() {
        //判断开关指示按钮状态
        if (toggleButton.isChecked()) {
            flashLight.turnOn();
        } else {
            flashLight.turnOff();
        }
        show();
    }

    public void show() {
        //根据flashlight属性控制界面
        if (flashLight.on) {
            linearLayout.setBackgroundColor(flashLight.color);
        } else {
            linearLayout.setBackgroundColor(Color.BLACK);
        }
    }
}

运行程序:
在这里插入图片描述

举例说明:创建一个Car类

我们来创建一个汽车类,它的属性有颜色color、品牌brand、速度speed。它的方法有前进、停止。
Car.java

public class Car {
    public String color;
    public String brand;
    public int speed;

    public void go(TextView tv) {
        tv.append("\n" + color + brand + "汽车以时速" + speed + "前进");
    }

    public void stop(TextView tv) {
        tv.append("\n" + color + brand + "汽车停止");
    }
}

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:text="创建汽车" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:text="go" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:text="stop" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="#222222" />
    
</LinearLayout>

MainActivity

public class MainActivity extends AppCompatActivity {
    Car car;

    Button create;
    Button go;
    Button stop;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        create = findViewById(R.id.button1);
        go = findViewById(R.id.button2);
        stop = findViewById(R.id.button3);
        textView = findViewById(R.id.textView);
    }

    public void doClick(View view) {
        switch (view.getId()) {
            case R.id.button1:
                f1();
                break;
            case R.id.button2:
                f2();
                break;
            case R.id.button3:
                f3();
                break;
        }
    }

    private void f1() {
        car = new Car();
        //默认值如下
        //color="";
        //brand="";
        //speed=0;
        car.color = "红色";
        car.brand = "BMW";
        car.speed = 80;
        textView.setText("汽车已创建");
    }

    private void f2() {
        car.go(textView);
    }

    private void f3() {
        car.stop(textView);
    }
}

运行程序:
在这里插入图片描述

类中包含的变量

一个类可以包含以下类型变量:
1、局部变量
在方法、构造方法或者语句块中定义的变量被称为局部变量。变量声明和初始化都是在方法中,方法结束后,变量就会自动销毁。
局部变量有以下特点:
①必须手动初始化(分配内存空间),没有默认值
②局部变量作用域,到它定义的代码块为止
③作用域内不能重复定义

void f1(){
	int a = 10;
	if(){
		int a = 9;//这样是不允许的,作用域范围内重复定义了
		print(a);
	}
}
void f1(){
	int a = 10;
	if(){
		print(a);
		int b = 100;
	}//此时b的作用范围已结束,可以再定义一个b,不是同一个
	int b = 1000;	
}

2、成员变量
成员变量是定义在类中,方法体之外的变量。这种变量在创建对象的时候实例化。成员变量可以被类中方法、构造方法和特定类的语句块访问。
成员变量有以下特点:
①定义在类中,自动初始化,有默认值

Int a;默认0
boolean b;默认false
int[] c;默认null

例如我们第一个例子 Soldier 类中,唯一编号 id,默认值就是 0。
②访问范围:类中都可以访问,根据访问范围设置,类外也可以访问

3、类变量
类变量也声明在类中,方法体之外,但必须声明为 static 类型。
关于关键字 static 后边的文章中会讲到

类中的方法

对类中方法不懂的可以查看:Java 方法

内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值