【Java全栈学习笔记-U1-day09】 类和对象
声明:Java全栈学习笔记是为整理当初学习Java时的学习笔记,以复习为目的而制作,仅提供大家交流学习,请勿用作他用
第八天是复习就未上传
文章目录
Java的面向对象:
面向对象基础(类、对象、属性、方法…)、面向对象高级部分(封装、继承、多态、接口)
课程目标:
-
类和对象
-
使用类和对象
-
理解封装
一、 类和对象
1.1 在现实生活中:万物皆对象(现实世界中的对象)
1.2 现实生活中的对象:用来描述客观事物的一个实体、由一组特征和形为构成
1.3 什么是类
类是模拟现实生活中对象。
类是一个模版。
类是一种数据类型。
1.4 什么是属性
现实生活对象具有的特征称为属性。
1.5 什么是方法
现实生活对象具有的行为称为方法。
1.6 定义|创建类的语法
访问修饰符 class 类名{
//定义一系列的属性和方法
}
//例如
public class Abc{
//定义一系列的属性和方法
}
注意:
a. 访问修饰符:
统一用public代替, 使用public关键类定义的类,类名必需和文件名一样
b. 类名首字母大写
1.7 定义属性的语法
访问修饰符 数据类型 属性名称 = 初始值;
//例如
public int a = 0;
1.8 定义方法的语法
访问修饰符 返回值类型 方法名称(参数列表){
//方法体...
return 返回值;
}
public boolean isTrue(int a){
//方法体...
return true;
}
注意:方法无返回值时,返回值类型为void
不带参数和返回值的方法:
public void 方法名(){
//方法体...
}
示例:定义车类
//定义车类 汽车类型
public class Car {
//定义一系列的属性和方法
//1.定义属性
public String brand; //品牌
public String color; //颜色
public int price; //价格
//定义车跑的方法
public void run(){
System.out.println("车正在行驶的过程中......");
}
//定义车载音乐
public void music(){
System.out.println("车载音乐播放中......");
}
}
二、使用类
2.1 创建对象(实例化过程)
//类名 对象名=new 类名();
Car car1=new Car(); //存储
2.2 使用对象的属性和方法
2.2.1 使用对象的属性
//对象名.属性名[=值];
2.2.2使用对象的方法
//对象名称.方法名称();
示例二:创建车对象
//使用类
public class Test {
public static void main(String[] args) {
//1.创建对象(实例化过程)
//类名 对象名 = new 类名();
Car car1 = new Car();
//2.使用对象的属性和方法
//2.1使用对象的属性
//对象名.属性名 = 值
car1.brand = "神龙";
car1.color = "黑色";
car1.price = 80;
System.out.println("车的品牌是:" + car1.brand);
System.out.println("车的颜色:" + car1.color);
System.out.println("车的价格是:" + car1.price + "万");
//2.2 使用对象的方法
//对象名称.方法名称();
car1.run();
car1.music();
}
}
三、this关键字
在类中使用this关键字,代表当前对象
四、理解封装
对象的属性和方法通常被封装在一起,共同体现事物的特性, 二者相辅相承,不能分割
五、(扩展)综合:理解属性的作用
示例:
//数学工具类
public class MathTools {
public int r; //属性
public void calArea(){
//求面积
double res = 3.14 * r * r;
System.out.println("输出面积是:" + res);
}
}
第二步:输入半径求面积
public class Test2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//编程方式:面向过程->面向对象->面向接口
//理解面向对象编程
//输入半径求圆的面积:面向过程
//1.输入半径
System.out.println("请输入半径:");
int r = input.nextInt();
//使用面向对象
MathTools tools = new MathTools();
//赋值半径
tools.r = r;
//求面积
tools.calArea();
}
}
作业
作业一 使用类的方式描述计算机。
提示:计算机的各部件可以作为类的属性,showInfo()方法用于显示输出计算机相关配置信息。计算机的主要部件包括CPU,主板,显示器,硬盘,内存等。
package homework;
public class Computer {
public String CPU; //CPU信息
public String GPU; //GPU信息
public String mainboard; //主板信息
public String displayer; //显示器信息
public String hard_disk; //硬盘信息
public String RAM; //内存信息
public void showInfo(){
System.out.println("CPU信息:" + CPU);
System.out.println("显卡信息:" + GPU);
System.out.println("主板信息:" + mainboard);
System.out.println("显示器信息:" + displayer);
System.out.println("硬盘信息:" + hard_disk);
System.out.println("内存信息:" + RAM);
}
public static void main(String[] args) {
Computer com = new Computer();
com.CPU = "11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz 2.30 GHz";
com.GPU = "显卡:NVIDIA GeForce RTX 3050 Laptop GPU";
com.mainboard = "主板:惠普 88F9(LPC Controller - 438B)";
com.hard_disk = "镁光MTFDKBA512TFH-1BC1AABHA(固态硬盘)";
com.RAM = "内存:16 GB ( 三星 DDR4 3200MHz 8GB x 2 )";
com.showInfo();
}
}
作业二 某公司要开发新游戏,请用面向对象的思想设计英雄类,怪物类和武器类。
编写测试类,创建英雄对象,怪物对象和武器对象,并输出各自的信息。其中设定分别如下。
英雄类
属性:英雄名字,生命值。
方法:输出基本信息。
public class Hero {
public String hero_name; //英雄名字
public int hero_hp; //生命值
public void hero_showInfo(){
System.out.println("英雄名字:" + hero_name);
System.out.println("英雄生命值:" + hero_hp);
}
}
怪物类
属性:怪物名字,生命值,类型。
方法:输出基本信息。
public class Monster {
public String mons_name; //怪物名字
public int mons_hp; //怪物生命值
public String mons_type; //怪物类型
public void mons_showInfo() {
System.out.println("怪物名字:" + mons_name);
System.out.println("怪物生命值:" + mons_hp);
System.out.println("怪物类型:" + mons_type);
}
}
武器类
属性:武器名字,攻击力。
方法:输出基本信息。
public class Weapon {
public String weap_name; //武器名字
public int attack_point; //武器攻击力
public void weap_showInfo(){
System.out.println("武器名字:" + weap_name);
System.out.println("武器攻击力:" + attack_point);
}
}
测试类:
public static void main(String[] args) {
//创建英雄对象
Hero hero = new Hero();
//创建英雄信息
hero.hero_name = "特利迦奥特曼";
hero.hero_hp = 10000;
hero.hero_showInfo();
System.out.println();
//创建怪物对象
Monster monster = new Monster();
monster.mons_name = "撒旦迪洛斯";
monster.mons_hp = 26000;
monster.mons_type = "机械怪兽";
monster.mons_showInfo();
System.out.println();
//创建武器对象
Weapon weapon = new Weapon();
weapon.weap_name = "圆环武装";
weapon.attack_point = 5000;
weapon.weap_showInfo();
}
}
作业三:
public class Customer {
public int point; //积分
public String card_type; //卡类型
public void show() {
System.out.println("积分:" + point + " 卡类型:" + card_type);
}
}
package homework.customer;
public class Point_Feedback {
public static void feedback(Customer customer) {
if (customer.card_type.equals("金卡")){
if (customer.point > 1000) {
System.out.println("回馈积分500分!");
}else{
System.out.println("无回馈积分!");
}
}
if (customer.card_type.equals("普卡")) {
if (customer.point > 5000) {
System.out.println("回馈积分500分!");
}else {
System.out.println("无回馈积分!");
}
}
}
public static void main(String[] args) {
Customer customer1 = new Customer();
customer1.point = 3050;
customer1.card_type = "金卡";
customer1.show();
feedback(customer1);
Customer customer2 = new Customer();
customer2.point = 5050;
customer2.card_type = "普卡";
customer2.show();
feedback(customer2);
Customer customer3 = new Customer();
customer3.point = 550;
customer3.card_type = "金卡";
customer3.show();
feedback(customer3);
Customer customer4 = new Customer();
customer4.point = 4050;
customer4.card_type = "普卡";
customer4.show();
feedback(customer4);
}
}
作业四 请用面向对象的方式求圆的面积。
【重点理解属性的作用:从类外接收信息到类的内部】
要求: 定义一个工具类Tool
属性: 半径 r
方法:显示该半径对应圆的积为多少 show
效果如下:
请输入圆的半径:2
半径为2圆的面积为:12.56
是否是继续操作y|n:
public class Tool {
public int r; //半径
public double area;
public void show(int r) {
area = 3.14 * r * r;
System.out.println("半径为" + r + "圆的面积为:" + area);
}
}
package homework.areatool;
import java.util.Scanner;
public class Circle_Area {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Tool tool = new Tool();
String choose = "";
do {
System.out.print("请输入圆的半径:");
tool.r = input.nextInt();
tool.show(tool.r);
System.out.print("是否是继续操作(y|n):");
choose = input.next();
} while (choose.equals("y"));
}
}
[选做]要求使用面向对象实现[选做]
教员要求张浩使用面向对象的思想编写一个计算器类(Calculator),可以实现两个整数的加,减,乘,除运算。如果你是张浩,准备如何实现?写出你的思路。
效果如下:
请输入第一个数:20
请输入第二个数:20
请输入操作符: [ → \rightarrow →有可能为-减 、/除、 *]
计算结果为:40
public class Calculator {
public double first; //第一个数
public double second; //第二个数
public String operator; //操作符
public double operation(double first, double second, String operator) {
if (operator.equals("+")) {
return first + second;
} else if (operator.equals("-")) {
return first - second;
} else if (operator.equals("*")) {
return first * second;
} else if (operator.equals("/")) {
return first / second;
} else {
return 0;
}
}
}
package homework.calculator;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String choose = "";
Calculator calculator = new Calculator();
do {
System.out.print("请输入第一个数:");
calculator.first = input.nextDouble();
System.out.print("请输入第二个数:");
calculator.second = input.nextDouble();
System.out.print("请输入操作符:");
calculator.operator = input.next();
if (calculator.operation(calculator.first, calculator.second, calculator.operator) == 0) {
System.out.println("输入有误,请重新输入!");
} else {
System.out.println("计算结果为:" + calculator.operation(calculator.first, calculator.second, calculator.operator));
}
System.out.print("是否继续操作?(y/n):");
choose = input.next();
System.out.println();
} while (choose.equals("y"));
}
}