面对对象
- 面对对象编程(Obiect-Oriented Programming,OOP)
- 面对对象编程的本质就是:以类的方式组织代码,以对象的组织(封装)数据。
- 抽象
- 三大特性:
- 封装
- 继承
- 多态
- 从认识论的角度考虑是先有对象后有类。对象,是具体的事物。类,是抽象的,是对对象的抽象
- 从代码运行角度考虑是先有类后有对象。类是对象的模版。
回顾方法及加深
- 方法的定义:
- 修饰符
- 返回值类型
- break(跳出switch,结束循环)和return(方法结束)
- 方法名(驼峰命名法,见名知意)
- 参数列表(参数类型,参数名)…可变行参数
- 异常抛出
public class Dem01 {
public static void main(String[] args) {
}
public String sayHello(){
return "sayHello";
}
public void print(){
return;
}
public int max(int a,int b ){
return a>b ? a:b;
}
public void readFile(String file) throws IOException{
}
- 方法的调用:递归
public class Dem02 {
public static void main(String[] args) {
Student.op();
}
}
输出结果:
万叶:总会有地上的生灵,敢于直面雷霆的微光
public class Student {
public static void op(){
System.out.println("万叶:总会有地上的生灵,敢于直面雷霆的微光");
}
}
public class Dem02 {
public static void main(String[] args) {
Student student = new Student();
student.op();
}
}
输出结果:
万叶:总会有地上的生灵,敢于直面雷霆的微光
public class Student {
public void op(){
System.out.println("万叶:总会有地上的生灵,敢于直面雷霆的微光");
}
}
public class Dem03 {
public static void main(String[] args) {
int add = Dem03.add(1,2);
System.out.println(add);
}
public static int add(int a ,int b) {
return a+b;
}
}
输出结果:
3
public class Dem03 {
public static void main(String[] args) {
Dem03 dem03 = new Dem03();
int add = dem03.add(1,2);
System.out.println(add);
}
public int add(int a ,int b) {
return a+b;
}
}
输出结果:
3
public class Dem04 {
public static void main(String[] args) {
int a =1;
System.out.println(a);
Dem04.change(a);
System.out.println(a);
}
public static void change(int a){
a=10;
}
}
输出结果:
1
1
public class Dem05 {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person.name);
Dem05.change(person);
System.out.println(person.name);
}
public static void change(Person person){
person.name="胡桃";
}
}
class Person{
String name;
}
输出结果:
null
胡桃