JAVA类和对象
1. 类(class)是对现实的抽象,形成一个模板;对象(object)是类的一个实例。
2. OOP,面向对象的编程即使用类组织代码,使用对象封装数据。
2.1. OOP的三大特性:封装,集成和多态;
2.2. 通常在一个项目中,只有一个Application类中写main方法,作为整个应用的入口;
2.3. 类的元素有且只有两种:属性(静态元素)和方法(动态元素);
2.4. 实例化对象的时候,使用new关键字,这个过程一方面在为对象申请内存空间,另一方面在进行一定的初始化工作;
2.5. 构造函数的特点:
1) 及时在一个类中,没有任何一个方法,也一定有一个默认的构造方法:public 类名()
2) 构造方法没有返回值,也不需要void,方法名与类名一致。
3) new关键字本质是在调用构造函数;
4) 在类中,可以定义多个不同参数的构造函数。注意:一旦定义了有参数的构造函数,那么无参数的(默认)的构造函数一定要显式的定义,否则调用使用new关键字调用无参构造函数的
时候会报错。
3. 封装:属性私有,get/set
package com.oop.demo3;
public class Student {
private String name;
private int age;
private char sex;
public Student() {
}
public Student(String name, int age, char sex) {
this.name = name;
this.age = age;
this.sex = 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 >0 && age < 110){
this.age = age;
}else{
this.age = 3;
}
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
}
package com.oop.demo3;
public class Application {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student("John", 30, 'M');
System.out.println(s1.getName());
System.out.println(s2.getName());
}
}
4. 继承:继承是一种类与类之间的关系,类与类的关系除了继承还有:依赖、组合、聚合等。
继承使用extends关键字,意为:扩展。子类(派生类)与父类是“is a”的关系。
修饰符:public protected default private; 通常,子类继承父类的public属性和方法;
Java的所有类都直接或者间接继承自Object类;
Java中只有单继承;
super:
1)调用父类的方法;如果super在子类的构造方法中调用父类的构造方法,那么super必须是第一行(默认不写也是第一行调用);
2)在子类的构造方法中,不能同时调用super构造方法和this构造方法,因为他们两个都要求“第一行”调用。
package com.oop.demo4;
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.speak();
student.eat();
student.sleep();
}
}
package com.oop.demo4;
public class Person {
private int age;
public Person(){
System.out.println("A person is initialed. ");
}
private void walk(){
System.out.println("Person is walking.");
}
protected void sleep(){
System.out.println("Person is sleeping.");
}
void eat(){
System.out.println("Person is eating.");
}
public void speak(){
System.out.println("I am a person.");
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age > 0 && age < 110){
this.age = age;
}
}
}
package com.oop.demo4;
public class Student extends Person{
public Student(){
System.out.println("A student is initialed.");
}
@Override
public void speak() {
System.out.println("I am a student.");
}
@Override
public void sleep() {
super.sleep();
System.out.println("Student is sleeping");
}
}
5. 方法的重写 override, 方法名,参数必须一致。
1)静态方法:父类的引用指向子类,父类对象仍然调用父类自己的方法。FatherClass a = new ChildClass(); a.method()仍然调用FatherClass的method。方法没有重写。
2)非静态方法,子类重写了父类的方法,方法会重写。FatherClass a = new ChildClass(); a.method()会调用ChildClass的method。
3)private方法不能保持private修饰重写,修饰符可以放大。public>protected>default>private
4)抛出的异常:范围可以缩小,不能放大。ClassNotFoundException --> Exception
5)final方法无法重写。
6. 多态:父类的引用指向子类的对象,调用方法时会动态的调用子类重写过的方法。
package com.oop.demo5;
public class Application {
public static void main(String[] args) {
GraphCard[] graphCards = new GraphCard[3];
graphCards[0] = new GraphCard();
graphCards[1] = new ATiCard();
graphCards[2] = new NvdiaCard();
for(GraphCard graphCard : graphCards){
System.out.println("This is a: " + graphCard.getName());
}
}
}
package com.oop.demo5;
public class NvdiaCard extends GraphCard {
public NvdiaCard() {
super("NvdiaCard");
}
}
package com.oop.demo5;
public class ATiCard extends GraphCard{
public ATiCard() {
super("ATiCard");
}
}
package com.oop.demo5;
public class GraphCard {
String name = "GraphCard";
public GraphCard(String name) {
this.name = name;
}
public GraphCard(){
}
public String getName() {
return name;
}
}
7. instanceof关键字与类型转换,父类的引用指向子类的对象,转换引用类型时使用“(子类类型)父类引用”的形式进行强制类型转换。
package com.oop.demo6;
public class Application {
public static void main(String[] args) {
Student s = new Student();
System.out.println(s instanceof Student);//true
System.out.println(s instanceof Person);//true
System.out.println(s instanceof Object);//true
//System.out.println(s instanceof String);//compile error
System.out.println("===================");
Person p = new Student();
System.out.println(p instanceof Student);//true
System.out.println(p instanceof Person);//true
System.out.println(p instanceof Object);//true
//System.out.println(p instanceof String);//compile error
System.out.println("===================");
Object o = new Person();
System.out.println(o instanceof Student);//false
System.out.println(o instanceof Person);//true
System.out.println(o instanceof Object);//true
System.out.println(o instanceof String);//false
s.study();
s.talk();
p.talk();
((Student)p).study();
Person p1 = new Person();
p1.talk();
((Student)p1).study();//Exception
}
}

浙公网安备 33010602011771号