(Object-Oriented Programming)
以类的方式组织代码,以对象的方式组织(封装)数据
break和return的区别
return代表方法结束
方法名的命名规则,小驼峰,见名知意。
参数列表:参数类型 参数名
异常抛出:(后面补充)
静态方法
非静态方法
package com.oop.demo01; public class demo02 { public static void main(String[] args) { Student.say(); System.out.println("==============================="); Student student = new Student();//非静态要实例化 student.talk(); } public static void a(){ //b(); static和类一起加载的,而非静态是类实例化之后才存在。所以不能直接调用b(); } public void b(){ } }
package com.oop.demo01; public class Student { //静态方法 public static void say(){ System.out.println("say something"); } //非静态方法 public void talk(){ System.out.println("非静态"); } }
值传递
package com.oop.demo01; //值传递 public class Demo04 { public static void main(String[] args) { int a = 1; System.out.println(a); Demo04.change(a); System.out.println(a);//输出之后还是原来的值 } public static void change(int val){ val = 10; //将输入变成10 } }
引用传递
package com.oop.demo01; //引用传递:对象,本质还是值传递 public class Demo05 { public static void main(String[] args) { Person person = new Person(); System.out.println(person.name); //null Demo05.change(person); System.out.println(person.name); //axin } public static void change(Person person){ person.name = "axin"; //指向具体的对象,可以改变属性 } } //定义了一个类,有一个属性叫name class Person{ String name; }
类数抽象的数据类型,表示一类实物的整体描述
对象是抽象概念的具体事例
public class Student { //属性 String name; int age; //方法 public void study(){ System.out.println(this.name + "在学习"); } }
public class Application { public static void main(String[] args) { //实例化抽象类 Student student = new Student(); Student student1 = new Student(); student.name = "XiaoMing"; student.age = 13; student1.name = "XiaoHong"; student1.age = 14; System.out.println(student.age); System.out.println(student1.name); } }