当前位置: 首页 > news >正文

Java---面向对象

一.面向对象

1.概念

1.1面向过程 C

 吃饭:动作为核心

 起身--》开门--》大量的逻辑判断

1.2面向对象 C++/Java/Python/Go

 目标:吃饭

 人(忽略)吃饭

站在人类的角度思考问题

2.什么是对象?

Object-->东西(万事万物皆是对象。)

2.1类

类:抽象,一个设计,对象的抽象

对象:具体,类的具体

类 -----> 对象

狗 ---> Dog ---> 抽象的

(两只耳朵,一只尾巴,四条腿,一张嘴,毛发)----特征

(陪伴(动作,行为))----职责 

Class ---> Type

public class Dog {//狗的信息int leg = 4;String hair = "金黄色";double price = 666.66;public void run(){System.out.println("我的狗跑的飞快!");}public void accompany(){System.out.println("带来快乐...");}public static void main(String[] args) {Dog myDog = new Dog();myDog.run();myDog.accompany();}
}

3.以冰箱(Fridge)为例

1.名字(Fridge)

2.属性、特征、域(field)、xxx变量

3.方法、函数、动作

3.1Java的注释

*类、方法

/****/

*行注释

//

*块注释

/***/
3.2冰箱的类

属性、方法

3.3执行冰箱操作

升高温度、降低温度工作

3.4异常情况

限制温度

import javax.swing.*;//冰箱类
public class Fridge {private String brand;private String color;private double size;private double weight;private double price;private int year;//保修年限private int temperature = 5;//温度private String company;//公司public String getBrand() {return brand;}//给汽车的brand设置品牌public void setBrand(String brand) {this.brand = brand;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public double getSize() {return size;}public void setSize(double size) {this.size = size;}public double getWeight() {return weight;}public void setWeight(double weight) {this.weight = weight;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public int getYear() {return year;}public void setYear(int year) {this.year = year;}public int getTemperature() {return temperature;}public void setTemperature(int temperature) {this.temperature = temperature;}public String getCompany() {return company;}public void setCompany(String company) {this.company = company;}//冰箱升高的温度方法// n 要升高的温度public void tempUp(int n){if (temperature+n > 20){JOptionPane.showMessageDialog(null,"温度超出冰箱范围,升温失败");}else {temperature += n;}}//冰箱降低的温度方法// n 要降低的温度public void tempDown(int n){if (temperature-n < -20){JOptionPane.showMessageDialog(null,"温度超出冰箱范围,降温失败");}else {temperature -= n;}}//显示当前冰箱的温度public void print(){System.out.println("当前温度:" + temperature);}public static void main(String[] args) {Fridge myFridge = new Fridge();myFridge.setBrand("无敌");System.out.println(myFridge.getBrand());System.out.println("冰箱启动");myFridge.print();while (true){String opt = JOptionPane.showInputDialog("请选择操作(1-升温,2-降温)");//String s = null;int n = 0;if ("1".equals(opt)) {//升温操作//String s = JOptionPane.showInputDialog("请输入要升高的度数");n = Integer.parseInt(JOptionPane.showInputDialog("请输入要升高的度数"));myFridge.tempUp(n);}else if ("2".equals(opt)){//降温操作//s = JOptionPane.showInputDialog("请输入要降低的度数");n = Integer.parseInt(JOptionPane.showInputDialog("请输入要降低的度数"));myFridge.tempDown(n);}myFridge.print();//判断是否退出操作int c = JOptionPane.showConfirmDialog(null,"是否继续进行");if (0 != c){JOptionPane.showMessageDialog(null,"操作完成,谢谢使用");break;}}}
}

4.面向对象描述类

4.1文字描述

4.2代码描述

4.3类图描述

5.访问控制符

5.1 public---公开类型

任何代码都能访问

5.2 private---私有的

private String brand;private String color;private double size;private double weight;private double price;private int year;//保修年限private int temperature = 5;//温度private String company;//公司

只有在本class内可以访问

6.属性操作

    public String getBrand() {return brand;}//给汽车的brand设置品牌public void setBrand(String brand) {this.brand = brand;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public double getSize() {return size;}public void setSize(double size) {this.size = size;}public double getWeight() {return weight;}public void setWeight(double weight) {this.weight = weight;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public int getYear() {return year;}public void setYear(int year) {this.year = year;}public int getTemperature() {return temperature;}public void setTemperature(int temperature) {this.temperature = temperature;}public String getCompany() {return company;}public void setCompany(String company) {this.company = company;}

setter / getter方式

通过setXXX(参数)存储

通过getXXX()取出数据

7.this关键字

 //给汽车的brand设置品牌public void setBrand(String brand) {this.brand = brand;}

this ----- 当前对象

this.brand = brand; //当前对象的属性brand,值为传入的变量brand的值


http://www.mrgr.cn/news/10051.html

相关文章:

  • 进程相关命令和函数
  • Linux云计算 |【第二阶段】SECURITY-DAY3
  • 伦敦金规则:止损让你有路可退
  • 临床试验中缺失数据的问题讨论
  • 【chips】个人笔记系列-SystemVerilog
  • 日撸Java三百行(day34:图的深度优先遍历)
  • Flask restful 前后端分离和 restful 定义
  • 趋动科技 OrionX on VMware 打造 AI 就绪平台
  • stm32之I2C通信协议
  • Swift中的蓝牙之舞:Core Bluetooth的精妙应用
  • JavaScript 中的 7 个新 Set 方法:`union()`、`intersection()`,以及其他 5 个
  • 聊聊场景及场景测试
  • 【STM32仿真】基于STM32单片机设计的秒表时钟计时器仿真系统——程序源码proteus仿真图设计文档演示视频等(文末工程资料下载)
  • ant-design 自定义选择框
  • APACHE NIFI学习之—编码解码功能
  • 2025届八股文:计算机网络高频重点面试题
  • 在CMD运行pip指令安装python的库时
  • 电商平台 API 接入的方式
  • CSRF简单介绍
  • kali 中文设置