面向对象编程的本质就是:以类的方式组织代码,以对象的组织(封装)数据。
值传递:传递对象的一个副本,即使副本被改变,也不会影响源对象,因为值传递的时候,实际上是将实参的值复制一份给形参。
引用传递:传递的并不是实际的对象,而是对象的引用,外部对引用对象的改变也会反映到源对象上,因为引用传递的时候,实际上是将实参的地址值复制一份给形参。
说明:对象传递(数组、类、接口)是引用传递,原始类型数据(整形、浮点型、字符型、布尔型)传递是值传递。
类与对象:
类是一个模板:抽象,对象是一个具体的实例
方法
定义,调用
对应的引用
引用类型:除了那八个基本类型都是
对象是通过引用来操作的:栈---->堆
属性:字段Field 成员变量
默认初始化:
数字:0 0.0
char: u0000
boolean: false
引用:null
修饰符 属性类型 属性名 = 属性值!
5.对象的创建和使用
6.类:
静态的属性 属性
动态的行为 方法
该露的露,该藏的藏
封装(数据的隐藏)
记住这句话就够了:属性私有,get/set
1.提供程序的安全性,保护数据
2.隐藏代码的实现细节
3.统一接口
4.系统可维护增加了
package oop;
import oop.Demo02.person;
import oop.Demo03.Pet;
import oop.Demo04.Student;
public class Application {
public static void main(String[] args) {
Student s1 = new Student();
s1.setName("人来人往");
System.out.println( s1.getName());
s1.setAge(9999);//不合法的
System.out.println(s1.getAge());
}
}
package oop.Demo04;
public class Student {
//名字 private:私有
private String name;
//年龄
private int age;
//性别
private char sex;
//提供一些可以操作这个属性的方法!
//提供一些public的get,set方法
//get 获得这个数据
public String getName(){
return this.name;
}
//set 给这个数据设置值
public void setName(String name){
this.name = name;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
if (age < 0||age>120) {
this.age = 3;
}else {
this.age = age;
}
}
}
//alt+insert
两个子类:
package oop.Demo05;
//学生 is 人:派生类,子类
//子类继承了父类,就会拥有父类的全部方法
public class Student extends Person {
}
package oop.Demo05;
//老师 is 人:派生类,子类
public class Teacher extends Person {
}
一个父类
package oop.Demo05;
//父类
public class Person {
//public
//private
//defult
//protected
private int money = 10000000;
public void speak(){
System.out.println("人来人往");
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
}
package oop;
import oop.Demo02.person;
import oop.Demo03.Pet;
import oop.Demo05.Student;
import oop.Demo05.Teacher;
public class Application {
public static void main(String[] args) {
Teacher teacher = new Teacher();
teacher.setMoney(10000);
System.out.println(teacher.getMoney());
teacher.speak();
Student student = new Student();
student.setMoney(10000);
System.out.println(student.getMoney());
student.speak();
}
}
在Java中,所有的类,都默认直接或间接继承object类
注意点:
Vs this:
代表的对象不同:
this:本身调用者这个对象
super:代表父类对象的引用
前提
this:没有继承也可以使用
super:只能在继承条件下使用
构造方法
this():调用的是本类的构造
super():调用的是父类的构造
package oop;
import oop.Demo02.person;
import oop.Demo03.Pet;
import oop.Demo05.Student;
import oop.Demo05.Teacher;
public class Application {
public static void main(String[] args) {
Student student = new Student();
//student.test1("人来人往");
//student.test1();
}
}
package oop.Demo05;
//学生 is 人:派生类,子类
//子类继承了父类,就会拥有父类的全部方法
public class Student extends Person {
//隐藏代码:调用了父类的无参构造
public Student() {
System.out.println("student constructor");
}
private String name ="fei";
public void print(){
System.out.println("student");
}
public void test(String name) {
System.out.println(name);
System.out.println(this.name);
System.out.println(super.name);
}
public void test1(){
print();
this.print();
super.print();
}
}
package oop.Demo05;
//父类
public class Person {
public Person() {
System.out.println("person constructor");
}
protected String name="Cheng";
//私有的东西无法被继承
public void print(){
System.out.println("person");
}
}
重写:需要有继承关系,子类重写父类的方法!
重写,子类的方法和父类必要一致;方法体不同!
为什么需要重写:
1.父类的功能,子类不一定需要,或者不一定满足!
ALT+Inesert: override;
package oop;
import oop.Demo02.person;
import oop.Demo03.Pet;
import oop.Demo05.A;
import oop.Demo05.B;
import oop.Demo05.Student;
import oop.Demo05.Teacher;
public class Application {
//静态方法和非静态的方法区别很大!
public static void main(String[] args) {
//方法的调用只和左边定义的数据类型有关
A a = new A();
a.test();
//父类的引用指向了子类
B b = new A();//子类重写了父类的方法
b.test();
}
}
package oop.Demo05;
public class A extends B {
@Override//注解:有功能的注释!
public void test() {
System.out.println("A=>test()");
}
}
package oop.Demo05;
//重写都是方法的重写,和属性无关
public class B {
public void test(){
System.out.println("B=>test()");
}
}
//结果为
A=>test()
A=>test()
package oop;
import oop.Demo06.Person;
import oop.Demo06.Student;
import oop.Demo06.Teacher;
public class Application {
public static void main(String[] args) {
//子类转换为父类,可能丢失自己的本来的一些方法!
Student student = new Student();
student.go();
Person person = student;
// person.go();
}
}
/*
Person A = new Student();
A.run();
Student a = (Student) A;
a.go();
Object object = new Student();
// System.out.println(X instanceof Y);//能不能编译通过
System.out.println(object instanceof Student);//true
System.out.println(object instanceof Person);//true
System.out.println(object instanceof Object);//true
System.out.println(object instanceof String);//false
System.out.println(object instanceof Teacher);//false
System.out.println("===============================================");
Person person = new Student();
System.out.println(object instanceof Student);//true
System.out.println(object instanceof Person);//true
System.out.println(object instanceof Object);//true
System.out.println(object instanceof String);//false
System.out.println(object instanceof Teacher);//false
System.out.println("===============================================");
Student student = new Student();
System.out.println(object instanceof Student);//true
System.out.println(object instanceof Person);//true
System.out.println(object instanceof Object);//true
System.out.println(object instanceof String);//false
System.out.println(object instanceof Teacher);//false
*/
package oop.Demo06;
public class Student extends Person {
public void go(){
System.out.println("go");
}
========================================================================
}
package oop.Demo06;
public class Teacher extends Person {
}
=================================================================
package oop.Demo06;
public class Person {
public void run(){
System.out.println("run");
}
}/*
多态注意事项
1.多态是方法的多态,属性没有多态
2.父类和子类,有联系 类型转换异常 ClassCastException!
3.存在条件:继承关系;方法需要重写,父类引用指向子类对象!
*/
被以下修饰符修饰过的方法不能被重写 也就不能被多态
static 方法,属于类,它不属于实例
final 常量;
private方法;私有
package oop;
import oop.Demo06.Person;
import oop.Demo06.Student;
public class Application {
public static void main(String[] args) {
//一个对象的实际类型是确定的
//new Student();
//new person();
//可以指向的引用类型就不确定了:父类的引用指向子类
//student能调用的方法都是自己的或者继承父类的
Student s1 = new Student();
//父类可以指向子类,但是不能调用子类独有的方法
Person s2 = new Student();
Object s3 = new Student();
//对象能执行那些方法,主要看对象左边的类型,和右边关系不大
//s2.eat();//子类重写了父类的方法,执行子类的方法
s1.eat();
}
}
package oop.Demo06;
public class Person {
public void run(){
System.out.println("renlairenwang");
}
}
/*
多态注意事项
1.多态是方法的多态,属性没有多态
2.父类和子类,有联系 类型转换异常 ClassCastException!
3.存在条件:继承关系;方法需要重写,父类引用指向子类对象!
*/
package oop.Demo06;
public class Student extends Person {
@Override
public void run() {
System.out.println("son");
}
public void eat(){
System.out.println("eat");
}
}