Java_面向对象入门
1.面向过程与面向对象
(1)面向过程:强调步骤
(2)面向对象:强调对象
(3)代码演示:
//要求打印格式为:[4,9,94,4654,1]
public class test{
public static void main(String[] args){
int[] array={4,9,94,4654,1};
//方法一:面向过程
System.out.print("[");
for(int i=0;i<array.length;i++){
if(i==array.length-1){//如果是最后一个元素
System.out.println(array[i]+"]");
}else{
System.out.print(array[i]+",");
}
}
System.out.println("==================");
//方法二:面向对象
System.out.println(Arrays.toString(array));
}
}
2.面向对象特点:封装,继承,多态
3.类与对象
(1)类(抽象):一组相关属性和行为的集合
(2)对象(具体):一类事物的具体体现,对象是类的一个实例
(3)关系:类是对象的模板,对象是类的实体
4.类的定义
(1)注意事项:
- 成员变量直接定义在类当中,在方法的外面
- 成员方法不需要static关键字
- 类不能直接使用,需要根据类创建一个对象,才能使用
(2)类的使用
- 导包
- 创建:类名称 对象名=new 类名称();
- 使用:使用成员变量(对象名.成员变量名);使用成员方法(对象名.成员方法名)
(3)代码演示
public class student {
//成员变量
String name;
int age;
//成员方法
public void eat(){
System.out.println("吃饭");
}
public void sleep(){
System.out.println("睡觉");
}
public void study(){
System.out.println("学习");
}
}
public class test{
public static void main(String[] args){
//创建对象
student stu=new student();
//使用成员变量
System.out.println(stu.name);//null
System.out.println(stu.age);//0
System.out.println("===============");
//改变对象中成员变量的内容
stu.name="zhangsan";
stu.age=90;
//使用成员变量
System.out.println(stu.name);//zhangsan
System.out.println(stu.age);//90
System.out.println("===============");
//使用成员方法
stu.eat(); //吃饭
stu.sleep(); //睡觉
stu.study(); //学习
}
}
5.对象的定义及应用
public class Phone {
//成员变量
String brand;
double price;
String color;
//成员方法
public void call(String who){
System.out.println("给"+who+"打电话");
}
public void sendMessage(){
System.out.println("群发短信");
}
}
public class test{
public static void main(String[] args){
//创建对象one
Phone one=new Phone();
//使用成员变量
System.out.println(one.brand);//null
System.out.println(one.price);//0.0
System.out.println(one.color);//null
System.out.println("============");
//修改成员变量
one.brand="苹果";
one.price=7999.0;
one.color="绿色";
//使用成员变量
System.out.println(one.brand);//苹果
System.out.println(one.price);//7999.0
System.out.println(one.color);//绿色
System.out.println("============");
//使用成员方法
one.call("张三");//给张三打电话
one.sendMessage();//群发短信
System.out.println("============");
//创建对象two
Phone two=new Phone();
//使用成员变量
System.out.println(two.brand);//null
System.out.println(two.price);//0.0
System.out.println(two.color);//null
System.out.println("============");
//修改成员变量
two.brand="华为";
two.price=5999.0;
two.color="红色";
//使用成员变量
System.out.println(two.brand);//华为
System.out.println(two.price);//5999.0
System.out.println(two.color);//红色
System.out.println("============");
//使用成员方法
two.call("李四");//给李四打电话
two.sendMessage();//群发短信
System.out.println("============");
//创建对象three
Phone three=two;
//使用成员变量
System.out.println(three.brand);//华为
System.out.println(three.price);//5999.0
System.out.println(three.color);//红色
System.out.println("============");
//修改成员变量
three.brand="小米";
three.price=6999.0;
three.color="银色";
//使用成员变量
System.out.println(three.brand);//小米
System.out.println(three.price);//6999.0
System.out.println(three.color);//银色
System.out.println("============");
//使用成员方法
three.call("王二虎");//给王二虎打电话
three.sendMessage();//群发短信
System.out.println("============");
}
}
6.使用对象类型作为方法的参数
(1)注意:当一个对象作为参数,传递到方法当中时,实际上传递进去的是对象的地址值
(2)代码演示:
public class Phone {
//成员变量
String brand;
double price;
String color;
}
public class test{
public static void main(String[] args){
Phone one=new Phone();
one.brand="苹果";
one.price=7999.0;
one.color="绿色";
method(one);
}
public static void method(Phone param){
System.out.println(param.brand);//苹果
System.out.println(param.price);//7999.0
System.out.println(param.color);//绿色
}
}
7.使用对象类型作为方法的返回值
(1)注意:当使用一个对象类型作为方法的返回值时,返回值其实就是对象的地址值
(2)代码演示:
public class Phone {
//成员变量
String brand;
double price;
String color;
}
public class test{
public static void main(String[] args){
Phone two=getPhone();
System.out.println(two.brand);//苹果
System.out.println(two.price);//7999.0
System.out.println(two.color);//绿色
}
public static Phone getPhone(){
Phone one=new Phone();
one.brand="苹果";
one.price=7999.0;
one.color="绿色";
return one;
}
}
8.局部变量成员变量的区别
(1)定义的位置
- 局部变量:在方法的内部
- 成员变量:在方法的外部,直接写在类当中
(2)作用的范围
- 局部变量:只有在方法中才可以使用
- 成员变量:整个类中通用
(3)默认值不一样
- 局部变量:没有默认值,必须手动赋值
- 成员变量:如果没有赋值,则会有默认值,规则与数组保持一致
(4)内存位置不一样
- 局部变量:位于栈内存
- 成员变量:位于堆内存
(5)生命周期不一样
- 局部变量:随着方法进栈而诞生,随着方法出栈而消失
- 成员变量:随着对象创建而诞生,随着对象回收而消失