面向对象思想的概述
package com.besttest.class1;import java.util.Arrays;/*面向过程:当需要实现一个功能的时候,每一个具体的步骤都要亲力亲为,详细处理每一个细节;面向对象:当需要实现一个功能的时候,不关心具体的步骤,而是找一个已经具有该功能的人,来帮我做事。*/public class DemoArrayParm {public static void main(String[] args) {//目的:打印数组格式为{1,2,3,4,5}int[] arr1={1,2,3,4,5};//使用面向对象//找一个JDK给我们提供好的Arrays类//其中有一个toString方法,直接就能把数组变成想要的格式的字符串System.out.println(Arrays.toString(arr1));}}
类的定义
package com.besttest.class1;/*定义一个类,用来模拟“学生”实物。其由两个部分组成:属性(是什么):姓名年龄行为(能做什么):吃饭睡觉学习对应到java类当中:成员变量(属性):String nameint age成员方法(行为):public void eat() {} -吃饭public void sleep() {} -睡觉public void study() {} -学习【注意事项】1.成员变量是直接定义在类当中2.成员方法不要写static关键字*/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("学习了!");}}
类的使用
package com.besttest.class1;/*通常情况下,一个类并不能直接使用,需要根据类创建一个对象才能使用1.导包:也就是支出需要使用的类在什么位置import 包名.类名称import com.besttest.class1.Student;【对于和当前类属于同一个包的情况,可以省略不写导包语句】2.创建:格式-类名 对象名 = new 类名();Student st= new Student();3.使用: 分两种情况使用成员变量:对象名.成员变量名;使用成员方法:对象名.成员方法名();*///import com.besttest.class1.Student;public class DemoStudy {public static void main(String[] args) {//1.导包:可以不用导包//2.创建:根据Student类创建了一个名为st的对象,将右侧的字符串赋值给st对象当中的name成员变量Student st= new Student();//3.使用:使用成员变量,未赋值会有默认值null、0、0.0等System.out.println(st.name="lxy");//使用成员方法st.study();}}
一个对象的内存图
package com.besttest.class1;public class Phone {String brand;double price;String color;public void call(String name){System.out.println("打电话给"+name);}public void sendMessage(){System.out.println("群发短信!");}}package com.besttest.class1;public class Demo01Phone {public static void main(String[] args) {Phone obj=new Phone();obj.brand="Apple";obj.color="black";obj.price=5999.99;System.out.println("我买了一个"+obj.color+"的"+obj.brand+"手机"+ obj.price+"元钱");obj.call("mama");obj.sendMessage();}}
两个对象使用同一个方法的内存图
package com.besttest.class1;public class Phone {String brand;double price;String color;public void call(String name){System.out.println("打电话给"+name);}public void sendMessage(){System.out.println("群发短信!");}}package com.besttest.class1;public class Demo01Phone {public static void main(String[] args) {Phone obj=new Phone();obj.brand="Apple";obj.color="black";obj.price=5999.99;System.out.println("我买了一个"+obj.color+"的"+obj.brand+"手机"+ obj.price+"元钱");obj.call("mama");obj.sendMessage();Phone obj2=new Phone();obj2.brand="华为";obj2.color="black";obj2.price=6999.99;System.out.println("我买了一个"+obj2.color+"的"+obj2.brand+"手机"+ obj2.price+"元钱");obj2.call("papa");obj2.sendMessage();}}
两个引用指向同一个对象的内存图
package com.besttest.class1;public class Phone {String brand;double price;String color;public void call(String name){System.out.println("打电话给"+name);}public void sendMessage(){System.out.println("群发短信!");}}package com.besttest.class1;public class Demo01Phone {public static void main(String[] args) {Phone obj=new Phone();obj.brand="Apple";obj.color="black";obj.price=5999.99;System.out.println("我买了一个"+obj.color+"的"+obj.brand+"手机"+ obj.price+"元钱");obj.call("mama");obj.sendMessage();//将obj当中保存的对象那个地址值赋值给obj2Phone obj2=obj;obj2.brand="华为";obj2.color="black";obj2.price=6999.99;System.out.println("我买了一个"+obj2.color+"的"+obj2.brand+"手机"+ obj2.price+"元钱");obj2.call("papa");obj2.sendMessage();}}
使用对象类型作为方法的参
当一个对象作为参数,传递当方法当中时,实际上传递进去的时对象的地址值
package com.demo01;public class Phone {String brand;int price;String color;}package com.demo01;public class Demo01PhoneParam {public static void main(String[] args) {Phone one =new Phone();one.brand="苹果";one.price=5888;one.color="玫瑰金";method(one);}public static void method(Phone param){System.out.println(param.brand);System.out.println(param.color);System.out.println(param.price);}}

使用对象类型作为方法的返回值
package com.demo01;public class Phone {String brand;int price;String color;}package com.demo01;public class PhoneReturn {public static void main(String[] args) {Phone two =getPhone();System.out.println(two.brand);System.out.println(two.color);System.out.println(two.price);}public static Phone getPhone(){Phone one = new Phone();one.brand="苹果";one.price=5888;one.color="玫瑰金";return one;}}
局部变量和成员变量区别
package com.demo01;import java.util.Enumeration;/*成员变量和局部变量的不同1.定义的位置不一样【重点】局部变量:在方法的内部成员变量:在方法的外部,直接写在类当中2.作用范围不一样【重点】局部变量:只有在放当中才可以使用,出了烦烦烦就不能再用成员变量:整改类全部可以使用3.默认值不一样【重点】局部变量:没有默认值,如果想要使用,必须手动赋值成员变量:如果没有赋值,会有默认值,规则和数组一样4.内存的位置不一样(了解)局部变量:位于栈内存成员变量:位于堆内存5.生命周期不一样(了解)局部变量:随着方法进栈而诞生,随着方法出栈而消失成员变量:随着对象创建而诞生,随着对象被垃圾回收而消失*/public class Demo01VariableDiffrence {String name; //成员变量public void method(){int num=20; //局部变量System.out.println(num);System.out.println(name);}public void method1(){// System.out.println(num); //错误用法System.out.println(name);}}
方法的封装
package com.demo01;/*面向对象三大特征:封装、继承、多态封装性在java当中的提现:1.方法就是一种封装2.关键字private也是一种封装*/public class Demo02Method {public static void main(String[] args) {int[] arr1={45,54,12,3,99};int maxone=getMax(arr1);System.out.println(maxone);}//方法封装public static int getMax(int[] array){int max= array[0];for (int i=0;i<array.length;i++){if (array[i]>max){max= array[i];}}return max;}}
关键字private封装
package com.demo01;/*问题描述:定义一个Person的年龄时,无法阻止不合理的数值设置进来解决方案:用private关键字将需要保护的成员变量进行修饰一旦使用了private进行修饰,那么本类当中任然可以随意访问。但是一旦超出了本类范围之外就不能再直接访问了间接访问private成员变量,就是定义一堆setter/getter方法必须叫steXxx或者getXxx命名规则。对于Getter来说,不能有参数,返回值类型和成员变量对应;对于Setter来说,不能有返回值,参数类型和成员变量对应。*/public class Person {String name;private int age;public void show(){System.out.println("我叫"+name+",我今年"+age+"岁!");}//这个成员方法专门用于获取age参数的数据public int getAge() {return age;}//这个成员方法专门用于向age设置数据public void setAge(int age) {if(age<0 || age>100){System.out.println("输入数据异常!");}else{this.age = age;}}}package com.demo01;public class Demo01Student {public static void main(String[] args) {Person per = new Person();per.setAge(30); //设置参数数据per.name="lxy";per.show();System.out.println("年龄:"+per.getAge()); //获取参数数据}}
this关键字的作用
package com.demo01;/*当方法的局部变量与类的成员变量重名的时候,根据”就近原则“,优先使用局部变量。如果需要访问类当中的成员变量,需要使用格式:this.成员变量名”通过谁调用的方法,谁就是this“*/public class Person1 {String name;//参数的name是对方名字//成员变量name是自己的名字public void sayHello(String name){System.out.println(name+",你好我是"+this.name);}}package com.demo01;public class Demo01Student {public static void main(String[] args) {Person1 per = new Person1();per.name="baby";per.sayHello("papa");}}
构造方法
package com.demo01;/*构造方法是专门用来创建对象的方法,当我们通过关键字new来创建对象时,其实就是调用的构造方法。格式:public 类名称(参数类型 参数名){方法体}注意事项:1.构造方法的名称必须和所在的类名称完全一样,就连大小写也要一样2.构造方法不要写返回值类型,脸void也不写3.构造方法不能return一个具体的返回值4.如果没有编写任何构造方法,那么编译器将会默认赠送一个构造方法,没有参数、方法体什么事情都不做5.一旦编写了至少一个构造方法,那么编译器将不再赠送6.构造方法也是可以进行重载的重载:方法名称相同,参数列表不同*/public class Student {//成员变量private String name;private int age;//无参数的构造方法public Student(){System.out.println("构造方法执行啦");}public Student(String name,int age){System.out.println("全参构造方法执行啦");this.name=name;this.age=age;}//Getter/Setterpublic String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}----------------------------------------------------------------------------------------------package com.demo01;public class Demo02Student {public static void main(String[] args) {Student stu1 =new Student(); //无参构造System.out.println("====================");Student stu2 = new Student("Lisa",24); //全参构造System.out.println("姓名:"+ stu2.getName()+",年龄:"+stu2.getAge());stu2.setAge(25);System.out.println("姓名:"+ stu2.getName()+",年龄:"+stu2.getAge());}}
写一个标准的类
package com.demo01;/*一个标准的类通常要拥有下面四个组成部分:1.所有的成员变量都要使用private关键字修饰;2.为每一个成员变量编写一对Getter/Setter方法;3.编写一个无参数构造方法;4.编写一个全参数构造方法。*/public class Student {//成员变量private String name;private int age;//无参数的构造方法public Student(){System.out.println("构造方法执行啦");}public Student(String name,int age){System.out.println("全参构造方法执行啦");this.name=name;this.age=age;}//Getter/Setterpublic String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
