面向对象
创建与初始化对象
Student类
package com. oop. Demo01;
public class Student {
String name;
int age;
public void study ( ) {
System. out. println ( this . name+ "在学习" ) ;
}
}
然后在主方法Application中调用
package com. oop. Demo01;
public class Application {
public static void main ( String[ ] args) {
Student xh = new Student ( ) ;
Student xm = new Student ( ) ;
xh. age = 10 ;
xm. age = 10 ;
xh. name = "小红" ;
xm. name = "小明" ;
System. out. println ( xh. name) ;
System. out. println ( xh. age) ;
xh. study ( ) ;
xm. study ( ) ;
}
}
运行结果:
类中的构造器(构造方法)
Person类
package com. oop. Demo01;
public class Person {
String name ;
int age;
public Person ( String name) {
this . name = name;
}
public Person ( ) {
}
public Person ( int age) {
this . age = age;
}
public Person ( String name, int age) {
this . name = name;
this . age = age;
}
}
Application
package com. oop. Demo01;
public class Application {
public static void main ( String[ ] args) {
Person person1 = new Person ( ) ;
Person person2 = new Person ( "小明" ) ;
Person person3 = new Person ( 12 ) ;
Person person4 = new Person ( "小红" , 13 ) ;
System. out. println ( "调用无参的构造器:" + person1. age) ;
System. out. println ( "调用有参的构造器String:" + person2. name) ;
System. out. println ( "调用有参的构造器int age:" + person3. age) ;
System. out. println ( "调用有参的构造器String name, int age:" + person4. name) ;
System. out. println ( "调用有参的构造器String name, int age:" + person4. age) ;
}
}
运行结果:
封装:Encapsulation
Encapsulation类
package com. oop. Demo01;
public class Encapsulation {
private String name;
private int age;
private char sex;
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
if ( age < 120 || age > 0 ) {
this . age = age;
} else
this . age = 3 ;
}
public char getSex ( ) {
return sex;
}
public void setSex ( char sex) {
this . sex = sex;
}
}
Application
package com. oop;
import com. oop. Demo01. Encapsulation;
import com. oop. Demo01. Person;
public class Application {
public static void main ( String[ ] args) {
Encapsulation el = new Encapsulation ( ) ;
el. setAge ( 99 ) ;
el. setName ( "王华" ) ;
System. out. println ( el. getAge ( ) ) ;
System. out. println ( el. getName ( ) ) ;
}
}
运行结果:
继承
代码演示:
Son 01类(子类)
package com. oop. Demo02;
public class Son01 extends Father {
public String name = "程儿子" ;
public Son01 ( ) {
super ( ) ;
System. out. println ( "儿子的无参构造器" ) ;
}
public void print ( String name) {
System. out. println ( name) ;
System. out. println ( this . name) ;
System. out. println ( super . getName ( ) ) ;
}
public void say ( ) {
System. out. println ( "程儿子会说话!" ) ;
}
public void test ( ) {
say ( ) ;
this . say ( ) ;
super . say ( ) ;
}
}
Father类(父类)
package com. oop. Demo02;
public class Father {
private String name = "程父亲" ;
private int moner = 1000000 ;
public Father ( ) {
System. out. println ( "父亲的无参构造器" ) ;
}
public void say ( ) {
System. out. println ( "程父亲会说话!" ) ;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getMoner ( ) {
return moner;
}
public void setMoner ( int moner) {
this . moner = moner;
}
}
Application类(启动)
package com. oop;
import com. oop. Demo01. Encapsulation;
import com. oop. Demo01. Person;
import com. oop. Demo02. Son01;
public class Application {
public static void main ( String[ ] args) {
Son01 son = new Son01 ( ) ;
son. say ( ) ;
System. out. println ( son. getMoner ( ) ) ;
son. print ( "孙中山" ) ;
son. test ( ) ;
}
}
运行结果:
小结
super注意点:
1. super调用父类的构造方法,必须在构造方法的第一个;
2. super必须只能出现在子类的方法或者构造方法中;
3. super和this不能同时调用构造方法;
Vs this:
代表对象不同:
this:本身调用者这个对象;
super:代表父类对象的应用;
前提:
this:没有继承也可以使用;
super:只能在继承的条件下才可以使用;
构造方法:
this():本类的构造;
super():父类的构造;
方法的重写
代码的演示
A(父类)
package com. oop. Override;
public class A {
public static void test1 ( ) {
System. out. println ( "A" ) ;
}
public void test2 ( ) {
System. out. println ( "AA" ) ;
}
}
B(子类)
package com. oop. Override;
public class B extends A {
public static void test1 ( ) {
System. out. println ( "B" ) ;
}
public void test2 ( ) {
System. out. println ( "BB" ) ;
}
}
运行结果
小结
重写:需要继承关系,子类重写父类的方法!
1. 方法名必须相同;
2. 参数列表必须相同;
3. 修饰符;范围可以扩大但不能缩小;public > Protected > Default > private;
重写:子类的方法和父类的方法一致,但方法体不同;
为什么需要重写?
1. 父类的功能,子类不一定需要,或者不一定满足!
重写快捷键:Alt+Insert——>override;