一.概念
1.1 面向过程和面向对象
面向过程 C
面向过程---以动作为核心
面向对象 C++/Java/Python/Go
面向对象---以目标为核心
站在人类的角度思考问题!!!
1.2什么是对象?
东西 (Object) 万物皆为对象
设计(抽象的)class --- 对象 ---工作
1.2.1 类
造一只小狗
package ff0414.com;
public class Dog {
int legs=4;
String hair="Golden";
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();
}
}
1.2.2 以冰箱为例
1.名字(Fridge)
package ff0414.com;
public class Fridge {
String brand;
String color;
double size;
double weight;
double price;
int year;//保修年份
static int temp;
String company;
public void tempUp(int n){
temp+=n;
}
public void tempDomn(int n){
temp-=n;
}
public static void main(String[] args) {
Fridge mf =new Fridge();
System.out.println("冰箱启动");
System.out.println("当前温度:" + temp+"℃");
}
}
改良版
public class Fridge {
String brand;
String color;
double size;
double weight;
double price;
int year;//保修年份
static int temp;
String company;
public void tempUp(int n){
temp+=n;
}
public void tempDown(int n){
temp-=n;
}
public void print()
{
System.out.println("当前温度:" + temp);
}
public static void main(String[] args) {
Fridge mf =new Fridge();
System.out.println("冰箱启动");
mf.print();
String s= JOptionPane.showInputDialog("请输入要升高温度");
int n=Integer.parseInt(s);
mf.tempUp(n);
mf.print();
s=JOptionPane.showInputDialog("请输入降低的温度");
n=Integer.parseInt(s);
mf.tempDown(n);
mf.print();
}
}
类,方法
行注释
块注释
1.3访问控制符
1.5.1 public---公开
公开(任何代码都能访问)
1.5.2 private--- 私有的
setxxx(参数)存储
getxxx(参数)取出数据
1.6 this关键字
this--当前对象
this.brand=brand;//当前对象的属性brand,值为传入的变量brand的值。
任务:创建一个类 手机类
属性自定义
方法自定义
set/get