类的继承是指在一个现有类的基础上去构建一个新的类,构建出来的新类被当作子类,现有的类被称作父类
使用extends关键字来声明一个类继承另一个类
一个类只能继承自一个父类,多个类可以继承父类,父类可以继承自其他父类,父类和子类是相对概念
调用子类构造器时会先调用父类的构造器
public class Person {
protected String name="person‘s name";
public Person()
{
System.out.println("Person无参构造执行了");
}
public void print()
{
System.out.println("person");
}
}
public class Student extends Person{
private String name="student‘s name";
public Student(){
//隐藏代码 调用父类构造器
System.out.println("Student无参构造执行了");
}
}
public class Demo {
public static void main(String[] args)
{
Student s1=new Student();
s1.test();
}
}
public class Person {
protected String name="person‘s name";
public Person()
{
System.out.println("Person无参构造执行了");
}
public void print()
{
System.out.println("person");
}
}
public class Student extends Person{
private String name="student‘s name";
public Student(){
//隐藏代码 调用父类构造器
System.out.println("Student无参构造执行了");
}
//重写了父类方法
public void print()
{
System.out.println("student");
}
}
public class Demo {
public static void main(String[] args)
{
Student s1=new Student();
//print被重写了,所以此时调用的是子类的print(),会输出student
s1.print();
}
}
当子类重写父类的方法后,子类对象将无法访问父类被重写的方法
super关键字可以用于访问父类的成员
public class Person {
protected String name="person‘s name";
public Person()
{
System.out.println("Person无参构造执行了");
}
public void print()
{
System.out.println("person");
}
}
public class Student extends Person{
private String name="student‘s name";
public Student(){
//隐藏代码 调用父类构造器
System.out.println("Student无参构造执行了");
}
public void print()
{
System.out.println("student");
}
public void test1(){
print();//student
this.print();//调用子类的构造方法,此时会输出student
super.print();//调用父类的构造方法,此时会输出person
}
public void test()
{
System.out.println(this.name);
System.out.println(super.name);
}
}
public class Demo {
public static void main(String[] args)
{
Student s1=new Student();
s1.test();
}
}
1.不能new这个类,只能靠子类去实现它
2.抽象类可以写普通的方法
3.抽象方法必须在抽象类中
//abstract 抽象类的关键字
public abstract class Action {
//约束,等待子类实现
//abstract抽象方法,只有方法的名字,没有方法的实现
public abstract void doSomething();
}
//继承自抽象类的方法,必须实现父类(抽象类)的所有方法,除非该类也是抽象类
public class A extends Action {
@Override
public void doSomething() {
}
}
接口与其他类的区别
interface
//interface 定义的关键字,接口都需要有实现类
public interface UserService {
//接口里定义的属性都是常量
int AGE=99;
//接口中的所有定义其实都是抽象的public abstract
void add(String name);
void delete(String name);
void update(String name);
void query(String name);
}
//类 可以实现接口 implements一个接口
//实现了接口中的类,必须要重写接口中的方法
public class UserServiceImp1 implements UserService {
@Override
public void add(String name) {
}
@Override
public void delete(String name) {
}
@Override
public void update(String name) {
}
@Override
public void query(String name) {
}
}
在同一个方法中,由于参数类型不同而导致执行效果各异的现象就是多态
为了实现多态,允许使用一个父类类型的变量来引用一个子类类型的对象,根据被引用子类对象特征的不同,得到不同的运行结果。
将子类对象当作父类对象时不需要任何显示声明,但是此时不能通过父类对象来调用子类的方法
将父类对象变成子类对象要向下转型,要显示声明
public class Demo {
static public void main(String[] args)
{
//高 低
Person person=new Student();
//子类转化为父类可能会丢失方法,此时obj不再能用StudentEat()
//Student将这个对象转化为Student类型,我们就可以使用Student类型的方法了
((Student)person).StudentEat();
//低 高
//Student student=new Person();这样转直接报错
Person person1=new Person();
//此时把person1转化为student,并赋值给student
Student student=(Student)person1;
}
}
public class Person {
public void run()
{
System.out.println("run");
}
public void PersonSay()
{
System.out.println("say");
}
}
public class Teacher2 extends Person{
void print()
{
System.out.println("teacher");
}
}
public class Student extends Person{
//重写了父类的run
public void run()
{
System.out.println("student run");
}
public void StudentEat()
{
System.out.println("eat");
}
}
用于判断一个对象是什么类型的(判断有没有类的继承关系)
public class Person {
public void run()
{
System.out.println("run");
}
}
public class Student extends Person{
//重写了父类的run
public void run()
{
System.out.println("student run");
}
public void eat()
{
System.out.println("eat");
}
}
public class Teacher2 extends Person{
void print()
{
System.out.println("teacher");
}
}
public class Demo {
static public void main(String[] args)
{
//x instanceof Y能不能编译通过看两者有没有关系,是ture就有继承关系,是false就同级
//Object>Person>Student
//Object>Person>Teacher
//Object>String
Object s1=new Student();
System.out.println(s1 instanceof Object); //有继承关系,输出true
System.out.println(s1 instanceof Person); //有继承关系,输出true
System.out.println(s1 instanceof Student); //有继承关系,输出true
System.out.println(s1 instanceof String); //有继承关系,输出true
System.out.println(s1 instanceof Teacher); //有继承关系,输出true
Person s2=new Student();
System.out.println(s2 instanceof Object); //有继承关系,输出true
System.out.println(s2 instanceof Person); //有继承关系,输出true
System.out.println(s2 instanceof Student); //有继承关系,输出true
System.out.println(s2 instanceof Teacher2); //student和teacher同级 输出false
// System.out.println(s2 instanceof String); //编译报错,没有关联
System.out.println();
Student s3=new Student();
System.out.println(s3 instanceof Object); //有继承关系,输出true
System.out.println(s3 instanceof Person); //有继承关系,输出true
System.out.println(s3 instanceof Student); //有继承关系,输出true
// System.out.println(s3 instanceof String); //无关 编译报错
// System.out.println(s3 instanceof Teacher2); //无关 编译报错
}
}
常用方法
方法名称 | 方法说明 |
---|---|
equals() | 指示其他某个对象是否与此对象"相等" |
getClass() | 返回Object的运行时类 |
hashCode() | 将对象的内存地址进行哈希运算,返回int类型的一个值 |
toString() | 返回该对象的字符串表示 |
在定义匿名内部类的地方往往直接创建一个该类的对象
public class Demo {
public static void main(String[] args) {
//定义一个内部类Cat实现Animal接口
class Cat implements Animal{
//实现shout方法
@Override
public void shout() {
System.out.println("miao~");
}
}
animalShout(new Cat());
}
//定义静态方法
//内部类Cat实现了Animal接口,在调用animalShout()方法时,将Cat类的实例对象作为参数传入方法中
public static void animalShout(Animal an){
an.shout();//调用传入对象an的shout方法
}
}
public interface Animal {
void shout();
}
检查性异常:最具代表性的检查性异常是用户错误或问题引起的异常,这是程序员无法预见的。例如要打开一个不存在文件时,一个异常就发生了,这些异常在编译时不能被简单地忽略
运行时异常:
错误Error:
Java把异常当作对象类处理,并定义一个类java.lang.Throwable作为所有异常的超类
异常类分为两大类,错误Error和异常Exception
抛出异常
捕获异常
异常处理五个关键字
try
catch
finally
//idea快速生成:ctrl+alt+t
public class demo1 {
public static void main(String[] args) {
int a=1;
int b=0;
try{//try监控区域
System.out.println(a/b);
}catch (ArithmeticException e){//catch捕获异常,里面的参是想要捕获的异常类型
System.out.println("程序出现异常,变量b不能为0");
e.printStackTrace();//打印错误的栈信息
}finally {//处理善后工作。可有可无,但是关闭的操作一般放在finally里面
System.out.println("finally");
}
}
}
throw
throws
public class demo1 {
public static void main(String[] args) {
try {
new demo1().test(1,0);
} catch (Exception e) {
e.printStackTrace();
}
}
public void test(int a,int b){
if(b==0){
throw new ArithmeticException();//主动抛出异常
}
//即使没有这行也会抛出异常
System.out.println(a/b);
}
}
使用Java内置的异常类可以描述在编程时出现的大部分异常情况。除此之外,用户还可以自定义异常。用户自定义异常类,只需继承Exception类即可
在程序中使用自定义异常类,大体可分为以下几个步骤:
1.创建自定义异常类
2.在方法中通过throw关键字抛出异常对象
3.如果在当前抛出异常的方法中处理异常,可以使用try-catch语句捕获并处理;否则在方法的声明处通过throws关键字指明要抛出给方法调用者的异常,继续进行下一步操作。
4.再出现异常方法的调用者中捕获并处理异常
public class MyException extends Exception{
//传递数字>10就抛出异常
private int detail;
public MyException(int a){
this.detail=a;
}
public String toString(){
return "MyException"+detail;
}
}
public class Test {
//可能会存在异常的方法
static void test(int a)throws MyException{
if(a>10){
throw new MyException(a);//抛出异常
}
System.out.println("ok");
}
public static void main(String[] args) throws MyException {
try {
test(11);
} catch (MyException e) {
//e.printStackTrace();
System.out.println("MyException"+e);
}
}
}
原文:https://www.cnblogs.com/MirandaYuen/p/14257984.html