1.继承性
多个类中有相同属性和行为时,可以将这些内容单独抽取到一个类中 继承的出现减少了代码冗余,提高了代码的复用性 继承的出现,有利于功能的拓展 继承是多态的前提 子类继承父类,就继承了父类的方法和属性 子类既可以使用父类的方法和属性,也可以自己创建新的数据和方法 子类不能直接访问父类中的私有成员变量和方法(可以通过父类中的public方法来间接访问) Java只支持单继承
public class ExtendDemo {
public static void main ( String[ ] args) {
Student student= new Student ( ) ;
student. age= 12 ;
student. name= "mike" ;
student. setSecret ( "big secret" ) ;
}
}
class People {
private String secret= "secret" ;
public String name;
public String getSecret ( )
{
return this . secret;
}
}
class Student extends People {
int age;
}
2.方法的重写
子类对父类的方法的重写,子类的方法会覆盖父类的方法 子类重写的方法和父类的方法必须有相同的方法名称,参数列表 子类重写的返回值不能大于父类方法的返回值 子类重写方法的访问权限不能小于父类方法的访问权限 子类方法抛出的异常不能大于父类被重写方法的异常 子类不能重写父类private修饰的方法 声明为static的方法改写不叫重写
package test;
import sun. net. www. content. text. plain;
public class ExtendDemo {
public static void main ( String[ ] args) {
Person person= new Person ( ) ;
person. name= "mike" ;
person. age= 12 ;
System. out. println ( person. getInfo ( ) ) ;
Student student= new Student ( ) ;
student. name= "tom" ;
student. age= 13 ;
student. school= "SchoolOne" ;
System. out. println ( student. getInfo ( ) ) ;
Person stu= new Student ( ) ;
stu. name= "tom" ;
stu. age= 13 ;
System. out. println ( stu. getInfo ( ) ) ;
}
}
class Person {
public String name;
public int age;
public String getInfo ( )
{
return name+ " " + age;
}
}
class Student extends Person {
public String school;
public String getInfo ( )
{
return name+ " " + age+ " " + school;
}
public void say ( )
{
System. out. println ( "I am a stdudent" ) ;
}
}
3.关键字super
super可以用于访问父类中定义的属性 子父类属性重名 super可以用于访问父类中定义的成员方法 子父类方法重名 super可以在子类构造器中调用父类的构造器
public class ExtendDemo {
public static void main ( String[ ] args) {
Student student= new Student ( ) ;
student. say ( ) ;
}
}
class Person {
public void say ( )
{
System. out. println ( "Father" ) ;
}
}
class Student extends Person {
public void say ( )
{
super . say ( ) ;
System. out. println ( "Child" ) ;
}
}
子类所有的构造器都会默认访问父类中空参的构造器 如果父类中没有空参构造器,子类的构造器必须指定自己的构造器或者指定父类的构造器,指定语句必须放在构造器首行 如果父类中没有无参构造器,并且子类没有显示调用自己或父类的构造器,会编译出错
public class ExtendDemo {
public static void main ( String[ ] args) {
Student student= new Student ( ) ;
}
}
class Person {
}
class Student extends Person {
public Student ( )
{
System. out. println ( "子类无参构造器" ) ;
}
}
public class ExtendDemo {
public static void main ( String[ ] args) {
Student student= new Student ( ) ;
}
}
class Person {
public Person ( )
{
}
}
class Student extends Person {
public Student ( )
{
System. out. println ( "子类无参构造器" ) ;
}
}
public class ExtendDemo {
public static void main ( String[ ] args) {
Student student= new Student ( ) ;
}
}
class Person {
String name;
public Person ( String name)
{
System. out. println ( "父类带参构造器" ) ;
}
}
class Student extends Person {
public Student ( )
{
super ( "tom" ) ;
System. out. println ( "子类无参构造器" ) ;
}
}
public class ExtendDemo {
public static void main ( String[ ] args) {
Student student= new Student ( 1 ) ;
}
}
class Person {
String name;
public Person ( String name)
{
System. out. println ( "父类带参构造器" ) ;
}
}
class Student extends Person {
int age;
public Student ( )
{
super ( "tom" ) ;
System. out. println ( "子类无参构造器" ) ;
}
public Student ( int age)
{
this ( ) ;
this . age= age;
}
}
4.子类对象实例化过程
package test;
public class ExtendDemo {
public static void main ( String[ ] args) {
new Wolf ( ) ;
}
}
class Creature {
public Creature ( )
{
System. out. println ( "Creatuer 无参数的构造器" ) ;
}
}
class Animal extends Creature {
public Animal ( String name)
{
System. out. println ( "Animal带一个参数的构造器,动物名字:" + name) ;
}
public Animal ( String name, int age)
{
this ( name) ;
System. out. println ( "Animal带两个参数的构造器,age:" + age) ;
}
}
class Wolf extends Animal {
public Wolf ( )
{
super ( "灰太狼" , 3 ) ;
System. out. println ( "Wolf无参的构造器" ) ;
}
}
Creatuer 无参数的构造器
Animal带一个参数的构造器,动物名字:灰太狼
Animal带两个参数的构造器,age: 3
Wolf无参的构造器
5. 多态性
体现:父类的引用指向子类的对象 编译时类型:由声明该变量时使用的类型决定 运行时类型:由运行时实际赋给该变量的对象决定 编译时:看左边 运行时:看右边 如果父类的引用指向子类的对象,该引用变量就不能再访问子类中新加的属性和方法(父类中没有) 注意:当父类和子类中有相同的属性和方法时,父类的引用指向子类的对象,引用访问的方法是子类重写的方法,但是成员变量却是父类引用自己的
public class ExtendDemo {
public static void main ( String[ ] args) {
People stu= new Student ( ) ;
System. out. println ( stu. name) ;
stu. say ( ) ;
stu. specialOfPeiole ( ) ;
}
}
class People {
String name= "people" ;
public void say ( )
{
System. out. println ( "I am a people" ) ;
}
public void specialOfPeiole ( )
{
System. out. println ( "specialOfPeople" ) ;
}
}
class Student extends People {
String name= "student" ;
public void say ( )
{
System. out. println ( "I am a student" ) ;
}
public void specialOfStudent ( )
{
System. out. println ( "specialOfStudent" ) ;
}
}
people
I am a student
specialOfPeople
6、 ==和equals的区别
==既可以比较基本类型,也可以比较引用类型。比较基本类型时,比较的时数值,比较引用类型时比较的时内存地址 equals时Object的方法。如果没有被重写过比较的也是==,但是像String的equals方法是被重写过的,比较的是内容
7、包装类的使用
基本数据类型 包装类 byte Byte short Short int Integer long Long float Float double Double boolean Boolean char Character
Integer x= new Integer ( 5 ) ;
Float f1= Float. parseFloat ( "12" ) ;
Integer x1= 12 ;
Integer x2= new Integer ( 13 ) ;
int x3= x2. intValue ( ) ;
Float f2= new Float ( 12.1 ) ;
float f3= f2. floatValue ( ) ;
int x4= x2;
float f4= f2;
String s= String. valueOf ( 12 ) ;
String s1= String. valueOf ( 12.1 ) ;
int a= Integer. parseInt ( "12" ) ;
float b= Float. parseFloat ( "12.1" ) ;
int a1= new Integer ( "12" ) ;
System. out. println ( a1) ;
Integer c= new Integer ( 4 ) ;
String cstr= c. toString ( ) ;