1 类和对象的关系
对象是根据类创建的实例
类是创建对象的模板/图纸
类是对现实中一类事物的描述
对象是现实事物在电脑中的模型
让汽车跑起来
- 创建一个类描述汽车类型
class Car{
//定义变量表示数据
double speed;
char color;
String number;
//定义方法表示功能
void run(){
sout("一辆"+color+"色的汽车,车牌是"+number+"的,正在以"+speed+"km/h的速度运行!");
}
}
- 根据类创建对象
Car c11=new Car();//根据类创建对象
Car c12=new Car();//根据类创建对象
Car c13=new Car();//根据类创建对象
//类中定义了那变量 对象就拥有那些数据
c11.number="豫A1111";c12.color='黑';
//类中定义了那些方法 对象就拥有那些功能
c11.run();
2 成员
成员:类的组成
类的成员: 用于描述数据的变量---成员变量
用于描述功能的方法---成员方法
类中定义那些成员变量 此类创建的对象就拥有那些变量(对象中的变量--属性)
变量:成员变量+局部变量
变量的作用域:取决于定义此保留的大括号
成员变量:类中定义的变量 ---作用域是整个类
局部变量:方法中定义的变量 ---当前方法
public class Demo01Variable {
/*成员变量和局部变量区别:
* 1 是否有默认:成员变量有默认初始值,但局部变量没有默认初始值
* 2 定义位置不同:成员变量定义在类中 局部变量定义在方法中
* 3 作用域不同:成员变量作用域是本类 局部本类作用域是当前方法
* */
public static void main(String[] args) {
// 调用一个类的方法:通过此类的对象类调用
Test01 t1=new Test01();
t1.t1(11);
}
}
class Test01{
int a;//成员变量
void t1(int b) {//局部变量
int c=1;//局部变量
System.out.println(a+":"+b+":"+c);
}
void t2() {
//System.out.println(a+":"+b+":"+c);
}
}
3 参数传递
public static void main(String[] args) {
int a=1;
int b=a;//b=1 a=1 吧变量a的值复制一份给了b
a++;//a=2 b=1
System.out.println("b="+b);
int[] arr1= {1,2,4};
int[] arr2=arr1;//arr2和arr1指向10行的数组对象
arr1[0]++;
System.out.println(arr2[0]);
//注意:基本数据类型变量之间传递的是常量值:
// int a=b;吧b的值复制一份给了a
// 引用数据类型变量之间传递的对象的内存地址
// int[] arr1=arr2; arr1和arr2指向同一个对象
int c=11;
testInt(c);//等价于:int a=c;a++
System.out.println("c="+c);
int[] arr= {1,2,3};
testArr(arr);//等价于:int[] array=arr;array[0]++;
printArr(arr);
}
//基本数据类型作为参数传递的是常量值:
static void testInt(int a) {
a++;
}
//引用数据类型作为参数传递的对象的内存地址
static void testArr(int[] array) {
array[0]++;
}
static void printArr(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+(i==arr.length-1?"\r\n":","));
}
}
4 方法重载:overload
概念
同一个类中 几个方法 方法名相同 参数列表不同的现象
public class Demo03Overload {
public static void main(String[] args) {
//实参:方法在调用时传递的数据
//形参:方法在定义时 定义的参数列表
//同一个类中 几个方法 方法名相同 参数列表不同的现象
//重载的方法:调用时 不是通过方法名来区分 而是通过参数类型来区分
//重载的方法:功能一致 只是需要的原始数据不同而已
//重载要求:1 同一个类 2方法名相同 3 参数列表不同(参数个数不同,参数类型不同,参数顺序不同)
// 其他方法声明无关
//方法由两部分组成:方法声明部分+方法体部分
Student3 s1=new Student3();
s1.add1(1, 2);
s1.add1(1, 2.1);
}
}
class Student3{
String name;
void show() {
System.out.println("show:::"+name);
}
//求和
int add1(int a,int b) {
System.out.println("int add1(int a,int b)");
return a+b;
}
double add1(int a,double b) {
System.out.println("double add1(int a,double b) ");
return a+b;
}
double add1(double a,int b) {
System.out.println("double add1(int a,double b) ");
return a+b;
}
int add1(int a,int b,int c) {return a+b+c;}
//重载的要求:
//static int add1(int a,int b,int c) {return 1;}
}
5 构造方法:constructor:构造器
概念
public class Demo04Constructor {
/*
* 构造方法:contructor:
* 类中定义的 用于构建创造对象的方法--构造方法
* 构造方法特点:
* 1 构造方法名字必须和类名一致
* 2 构造方法没有返回值 不用void标识
* 3 构造方法被new 调用,调用一次就创建一个对象
* 4 如果一个类没有构造方法 编译器默认添加一个无参数的构造方法
* 5 构造方法不能被对象调用
* 6 构造方法的参数列表 一般用于给成员变量赋值
*
* */
public static void main(String[] args) {
//构建类创建对象:对象类型 引用名=new 对象类型();
Student4 s1=new Student4();
s1=new Student4();
//分析:创建对象:new Student4();
//new是创建对象的关键字
//Student4():()是方法的关键字 说明这是个方法
//Student4()方法在Student4中没有看到 但是可以使用 说明默认有方法Student4()
//s1.show();
}
}
class Student4{
int age;
void show() {
System.out.println("show::"+age);
//return;
}
Student4() {
System.out.println("Student4() ");
}
Student4(int a) {
age=a;
System.out.println("Student4(int a) ");
}
Student4(int a,int b) {
System.out.println("Student4(int a,int b) ");
}
void Student4() {//warn:This method has a constructor name
System.out.println("void Student4() ");
}
}
构造方法和普通方法的区别
1:格式不同:普通方法必须有返回值类型标识 如果没有返回值用void标识
构造方法没有返回值 不用void标识
2:命名不同:构造方法名字必须是类名
普通方法名字可以是类名(但如果是类名 不规范)
3:调用不同:构造方法被关键字new调用 调用一次就创建一个新的对象
普通方法被对象调用 一个对象可以调用多次普通方法
4:意义不同:构造方法功能用于创建对象
普通方法表示当前类创建的对象具有的功能
6 给对象的属性赋值方式
public class Demo05Set {
public static void main(String[] args) {
Car5 c11=new Car5();
Car5 c12=new Car5();
Car5 c13=new Car5();
//给对象的属性赋值:
c11.color='红';//2:对象.属性 给属性赋值:特点:必须先创建对象 只能更改当前对象的属性值:::类似于自己喷漆
//c11.money=-1; //此方式被禁止:类无法控制值的范围
Car5 c14=new Car5("豫A88888");//3:通过构造方法参数列表给属性赋值:特点:对象一创建属性就有指定值 只能更改当前对象的属性值:::出场定制
c11.show();
c12.show();
c13.setSpeed(-100);//4:通过普通方法参数列表给属性赋值:特点:必须先创建对象 只能更改当前对象的属性值:::类似于4s店改装
c13.show();
c14.show();
}
}
class Car5{
String number;
char color;
float money=100000;//1:在类中直接给成员变量赋值 特点:此类创建的对象的此属性默认都是此值 类似于修改图纸
int speed;
void show() {
System.out.println(color+":"+number+":"+money+":"+speed);
}
Car5(){}
Car5(String n){number=n+"0000";}
void setSpeed(int s) {
if(s<0) {return;}
speed=s+1;
}
}
7 this
public class Demo06This {
/*this:关键字 表示的当前对象:::类似于现实中的我自己
*使用场景1: 成员变量与局部变量重名时 方法中的变量名指向的局部变量
* 通过this.变量名来指向成员变量
* 注意:方法中调用成员 前面默认有this.
*使用场景2:构造方法之间的相互调用 通过this(参数列表)
* 注意: this(参数列表)必须是第一个语句
*
* */
public static void main(String[] args) {
// Demo06 d1=new Demo06();
// d1.show();
// System.out.println(d1.a);
//
// Demo06 d2=new Demo06();
// d2.a=11;
// d2.show();
new Demo06(1);
}
}
class Demo06{
int a=1;//成员变量
int b=11;
void show() {
int a=2;//局部变量
System.out.println("a="+a);//a=2 就近原则:局部变量a
System.out.println("this.a="+this.a);//a=1 当前对象的a
System.out.println("b="+b);
System.out.println("this.b="+this.b);
hehe();
this.hehe();
}
void hehe() {
System.out.println("-----");
}
Demo06(){
System.out.println("我是构造方法——————");
}
Demo06(int a){
this();//Constructor call must be the first statement in a constructor
this.a=a;
//System.out.println("我是构造方法——————");
}
Demo06(int a,int b,int c,int d,int e){
}
Demo06(int a,int b,int c,int d,int e,int f){
this(a,b,c,d,e);
//this.f=f;
}
}
8 创建对象内存图
学习方法:
语法学习:记忆 特点:内容多 基础 乱
oop学习:1 理解概念
2 5 张内存图
创建对象内存图
8 static
关键字:修饰符:静态的
public class Demo02Static {
/*static: 修饰符 静态的
*static修饰变量:共享数据
*1 static修饰的变量 不但可以被对象调用 还可以被类名直接调用
*2 static修饰的变量是共享数据 只有一份 一旦更改 所有对象的此属性都被更改
*static修饰方法:
*1 static修饰的方法 不但可以被对象调用 还可以被类名直接调用
*2 static修饰的方法 只能调用static成员 不能调用非static成员
*
*
*类成员:static修饰的成员(成员变量+成员方法) ---属于类的
*实例成员:非static修饰的成员 ---属于对象的
*
*类变量:static修饰的变量----共享数据
*实例变量:非static修饰的变量----只能被当前对象调用
*类方法:static修饰的方法
*实例方法:非static修饰的方法
*
*类加载时:会为本类在内存中定义一个区域 作为静态区域:加载本类的静态成员
*
*什么情况下把一个变量定义为静态变量:数据是共享数据 只有一份 需要一变都变时
*什么情况下把一个方法定义为静态方法:当一个方法不涉及实例成员时 建议定义为静态方法
*
*
*
*
* */
public static void main(String[] args) {
// // TODO Auto-generated method stub
// Student02 s11=new Student02();
// s11.name="韩梅梅1";s11.banJi="java43";s11.ysjLogo="小天鹅";s11.guoJi="中国";
// Student02 s12=new Student02();
// s12.name="韩梅梅2";s12.banJi="java43";s12.ysjLogo="小天鹅";s12.guoJi="中国";
// Student02 s13=new Student02();
// s13.name="韩梅梅3";s13.banJi="java43";s13.ysjLogo="小天鹅";s13.guoJi="中国";
// Student02 s14=new Student02();
// s14.name="韩梅梅4";s14.banJi="java43";s14.ysjLogo="小天鹅";s14.guoJi="中国";
// //每个对象都拥有一套类中定义的只属于的自己的成员::每个对象都有自己的name,age,banJI和show
// //The static field Student02.guoJi should be accessed in a static way
// Student02.guoJi="中华人民共和国";
// s11.guoJi="中华民国";
// s11.show();
// s12.show();
// s13.show();
// s14.show();
// s11.hehe();
// s11.haha();
Student02.haha();
}
}
class Student02{
static void haha() {
System.out.println("static void haha()");
//hehe();//Cannot make a static reference to the non-static method hehe() from the type Student02
//System.out.println("学生:"+name+","+guoJi+","+age+","+banJi+","+ysjLogo);
// Cannot make a static reference to the non-static field banJi
}
String name;
int age;
static String guoJi;
String banJi;
String ysjLogo;
Student02(){}
void show() {
System.out.println("学生:"+name+","+guoJi+","+age+","+banJi+","+ysjLogo);
}
void hehe() {
System.out.println("void hehe()");
haha();
System.out.println("学生:"+name+","+guoJi+","+age+","+banJi+","+ysjLogo);
}
}