面向对象之关键字
1.this
this可以调用类的成员变量和成员方法,也可调用类的构造方法。
(1)this能出现在实例方法和构造方法中;
(2)this的语法是“this.”和“this()”;
(3)this不能出现在静态方法中;
(4)this大部分情况下是可以省略的;
package cz.oneday.csdn;
public class ThisTest {
//this调用成员变量
String name="cz";
public void showName(String name){
System.out.println("无this关键字"+name);
System.out.println("有this关键字"+this.name);
}
int eggCount;
//this调用构造方法
public ThisTest(int eggCount){
this.eggCount=eggCount;
System.out.println("鸡蛋有"+eggCount+"个");
}
//this调用类方法
public ThisTest(){
this(1);
}
public static void main(String[] args) {
ThisTest c=new ThisTest();
ThisTest c1=new ThisTest(5);
c.showName("czz");
}
}
2.super
super关键字代表的就是“当前对象”的那部分父类型特征。
(1)super能出现在实例方法和构造方法中。
(2)super的语法是“super.”和“super()”。
(3) super不能出现在静态方法中。
(4) super大部分情况下是可以省略的。
package cz.oneday.csdn;
public class SuperTest {
public static void main(String[] args) {
new Cat();
}
}
//父类,Animal类
class Animal {
//构造函数
public Animal() {
super();
System.out.println("1:Animal类的无参数构造函数执行");
}
public Animal(int i) {
super();
System.out.println("2:Animal类的有int参数构造函数执行");
}
}
//子类,Cat类
class Cat extends Animal{
//构造函数
public Cat() {
this("");
System.out.println("3:Cat类的无参数构造函数执行");
}
public Cat(String str) {
super(5);
System.out.println("4:Cat类的有String参数构造函数执行");
}
}
3.instanceof
instanceof关键字的作用是判断一个对象是否是一个具体类的实例,我们在重写equals方法中要先判断是否是同一对象,之后再判断一个对象是否是另一个的实例,如果是判断各个属性值以判断是否是同一对象,不是一定不是同一对象。
package cz.oneday.csdn;
public class InstanceofTest {
public static void main(String[] args){
People p = new People();
Tom t = new Tom();
System.out.println(p instanceof Person);
System.out.println(p instanceof Tom);
System.out.println(t instanceof Person);
System.out.println(t instanceof People);
}
}
interface Person{
public void eat();
}
class People implements Person{
@Override
public void eat(){
System.out.println("正在吃");
}
}
class Tom extends People{
@Override
public void eat(){
System.out.println("Tom正在吃");
}
}
4.static
1.修饰成员变量 2.修饰成员方法 3.静态块 4.静态导包。
package cz.oneday.csdn;
public class StaticTest {
int num;
static int numst;
public void method(){
System.out.println("成员方法");
System.out.println(num);
System.out.println(numst);
}
public static void methodstatic(){
System.out.println("static方法");
System.out.println(numst);
//静态不能直接访问非静态
// System.out.println(num);
//静态方法中不能使用this关键字
//System.out.println(this);
}
public static void main(String[] args) {
StaticTest obj=new StaticTest();
obj.method();
obj.methodstatic();//错
StaticTest.methodstatic();//对
mythod();
}
public static void mythod(){
System.out.println("自己的方法");
}
}
1.一旦使用 static 修饰成员方法,那么这就成为了静态方法。静态方法不属于对象,而是属于类的。
2.如果没有 static 关键字,那么必须首先创建对象,然后通过对象才能使用它。
3.如果有了 static 关键字,那么不需要创建对象,直接就能通过类名称来使用它。
4.无论是成员变量,还是成员方法。如果有了 static,都推荐使用类名称进行调用。
静态变量:类名称 . 静态变量
静态方法:类名称 . 静态方法()
5.注意事项:
静态不能直接访问非静态。
原因:因为在内存当中是【先】有的静态内容,【后】有的非静态内容。
“先人不知道后人,但后人知道先人”
静态方法当中不能用 this。
原因:this 代表当前对象,通过谁调用的方法,谁就是当前对象。
5.final
package cz.oneday.csdn;
public class FinalTest {
public static void main(String[] args) {
final byte num1 = 2,num2 = 3;
byte num = num1 + num2;
System.out.println(num);
}
}
(1).final修饰类中的属性或者变量
无论属性是基本类型还是引用类型,final所起的作用都是变量里面存放的“值”不能变。
(2).final修饰类中的方法
作用:可以被继承,但继承后不能被重写。
(3).final修饰类
作用:类不可以被继承。
(4).final修饰局部变量
系统不会为局部变量进行初始化,局部变量必须由程序员显示初始化。因此使用final修饰局部变量时,即可以在定义时指定默认值(后面的代码不能对变量再赋值),也可以不指定默认值,而在后面的代码中对final变量赋初值(仅一次)。
6.abstract
1.abstract:抽象的
2.abstract可以用来修饰的结构:类、方法
3.abstract修饰类:抽象类
> 此类不能实例化
> 抽象类中一定要构造器,便于子类实例化时调用(涉及,子类对象实例化的全过程)
> 开发中,都会提供抽象类的子类,让子类对象实例化,完成相关的操作
4.abstract修饰方法,抽象方法
> 抽象方法只有方法的声明,没有方法
> 包含抽象方法的类,一定是一个抽象类。反之,抽象类中可以没有抽象方法的
> 若子类重写了父类中的所有的抽象方法后,此子类可实例化
若子类没有重写父类中的所有的抽象方法,则此子类也是一个抽象类,需要使用abstract修饰
abstract 使用的注意点:
1.abstract不能用来修饰:属性、构造器等结构
2.abstract不能用来修饰私有方法、静态方法、final的方法、final的类
package cz.oneday.csdn;
public class AbstractTest {
public static void main(String[] args) {
// 一旦Person抽象了,就不可实例化
// Person p1 =new Person();
// p1.eat();
Student s = new Student("ss",20);
s.ss();
}
}
abstract class Creature{
public abstract void breath();//创建抽象方法
public void ss() {
System.out.println("aaa");
}
}
abstract class Person extends Creature{
String name;
int age;
public Person() { }//创建空参构造器
public Person(String name,int age) {
this.name = name;
this.age = age;
}
//不是抽象方法
// public void eat() {
//
// }
//抽象方法
// public abstract void eat();
public void walk() {
System.out.println("人走路");
}
}
class Student extends Person{
public Student (String name,int age) {
super(name,age);
}
//重写抽象方法
public void breath() {
System.out.println("呼吸哈哈哈 ");
}
// public void eat() {
// System.out.println("333");
// }
}
7.interface
1.接口不能直接使用,必须有一个“实现类”来“实现”该接口。
格式:public class 实现类名称 implements 接口名称 {
// …
}
2. 接口的实现类必须覆盖重写(实现)接口中所有的抽象方法。
实现:去掉abstract关键字,加上方法体大括号。
3.创建实现类的对象,进行使用。
package cz.oneday.csdn;
public interface MyinterfaceAbstract {
public abstract void methodAbs();//固定关键字
}
//实现类
public abstract class MyinterfaceAbstractImpl implements MyinterfaceAbstract {
@Override
public void abs1() {
System.out.println("这是第1个方法");
}
@Override
public void abs2() {
System.out.println("这是第2个方法");
}
@Override
public void abs3() {
System.out.println("这是第3个方法");
}
@Override
public void abs4() {
System.out.println("这是第4个方法");
}
}
8. 抽象类和接口的异同:
相同:
1、都有抽象方法。
2、都不能创建实例。
3、子类继承其后都必须全部实现其内的抽象方法,如果没有全部实现的话,类只能定义成抽象类。
不相同:
1、接口里只能有抽象方法,抽象类里可以有普通方法。
2、接口类里的属性默认有修饰语publicstaticfinal,但是抽象类中的属性可以是普通的。
3、接口没有初始化块,抽象类里有。
4、接口可以有多个父接口,但是抽象类只能有一个父类。