找回密码
 立即注册
本季先要点回忆了方法的重载与覆写、super与this关键词的区别。以后首要以实例讲解为主,首要回忆了JAVA中的承继及数组的基本概念,以后又讲解了JAVA中承继的图形表明。
上季内容回忆:
1、承继的使用和概念,承继的各种限制
2、子类对象的实例化过程
3、方法覆写
4、super的使用
有两个标题在面试中经常会涉及到哈~~~
面试一:解释一下方法的覆写与方法的重载的区别:
面试二:super与this的区别
属性覆写(较少使用)
我们来简单验证下哈
classA
{
Stringname=redking;
};
classBextendsA
{
//子类定义了一个和父类中一样的name属性
Stringname=Michael;
publicvoidprint()
{
//与System.out.println(this.name);作用一样
System.out.println(name);
}
};
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
Bb=newB();
b.print();
}
};
程序打印了在子类中定义的name属性:Michael
假如要打印父类中的name属性,我们可以修改成super.name
classA
{
Stringname=redking;
};
classBextendsA
{
//子类定义了一个和父类中一样的name属性
Stringname=Michael;
publicvoidprint()
{
//与System.out.println(this.name);作用一样
System.out.println(name);
//假如要打印父类中的name属性,我们可以修改成super.name
System.out.println(super.name);
}
};
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
Bb=newB();
b.print();
}
};
父类中的name属性输出了哈~这就叫属性的复写
属性通常状况下都要求被封装的,被封装以后必定子类是无法看见父类中的内容,所以基本就无法覆写。
super与this调用结构方法能一起写在一个结构方法之中吗?答案是不可哈~
Super调用结构方法时,一定要放在结构方法的首行,this调用结构方法时也有必要放在首行,假如两个都放在首行,则必定抵触。
有人以为可以不调用super方法哈,我们看下面的Demo02
classA
{
publicA(){}
};
classBextendsA
{
//里边有三个结构方法
publicB()
{
this(abc,888);
}
publicB(Stringname)
{
this();
}
publicB(Stringname,intage)
{
this(name);
}
};
这样就产生了一个问题,我们在讲this关键词时也是提到过的哈~
在使用this()调用结构方法的时候必定要留下一个出口。不然编译通不过哈~
classA
{
publicA(){}
};
classBextendsA
{
//里边有三个结构方法
publicB()
{
//最好把this(abc,888);修改成super()作为出口哈~
super();
}
publicB(Stringname)
{
this();
}
publicB(Stringname,intage)
{
this(name);
}
};
如今编译就通过了哈~
本季首要知识点:
1、承继的类图表明
2、承继的标题
如今我们来看个Demo03:
classA
{
};
classBextendsA
{
};
这个滴承继联系我们来用类图表明
认识了上面的内容,我们来看下练习题:
classPerson
{
privateStringname;
privateStringaddr;
privatecharsex;
privateintage;
//通常参数少的结构方法写在上面哈~~
publicPerson(){}
publicPerson(Stringname,Stringaddr)
{
this.setName(name);
this.setAddr(addr);
this.setSex('男');
this.setAge(27);
}
publicPerson(Stringname,Stringaddr,charsex,intage)
{
this.setName(name);
this.setAddr(addr);
this.setSex(sex);
this.setAge(age);
}
publicvoidsetName(Stringname)
{
this.name=name;
}
publicvoidsetAddr(Stringaddr)
{
this.addr=addr;
}
//M:表明男;F:表明女
publicvoidsetSex(charsex)
{
this.sex=sex;
}
publicvoidsetAge(intage)
{
this.age=age;
}
publicStringgetName()
{
returnthis.name;
}
publicStringgetAddr()
{
returnthis.addr;
}
publicchargetSex()
{
returnthis.sex;
}
publicintgetAge()
{
returnthis.age;
}
//一切的内容应当交给外部输出
publicStringgetInfo()
{
return名字:+this.name
+,地址:+this.addr
+,性别:+(this.sex=='M'?男:女)
+,年纪:+this.age;
}
};
classStudentextendsPerson
{
privatefloatmath;
privatefloatenglish;
publicStudent()
{
//默许隐含了super();
super();
}
publicStudent(Stringname,Stringaddr)
{
super(name,addr);
}
publicStudent(Stringname,Stringaddr,charsex,intage,floatmath,floatenglish)
[零基础学JAVA]JavaSE面向对象部分-14.面向对象高档(02).doc

(The first point of this season is to recall the difference between method overloading and overriding, super and this keyword. In the future, it will be mainly explained by examples, and the basic concepts of inheritance and arrays in JAVA will be recalled first, and then the graphical representation of inheritance in JAVA will be explained later.
Memories from last season:
1. The use and concept of inheritance, various limitations of inheritance
2. The instantiation process of the subclass object
3. Method Override
4. Use of super
There are two titles that are often involved in interviews~~~
Interview 1: Explain the difference between method overriding and method overloading:
Interview 2: The difference between super and this
property override (less used)
Let's just verify
classA
{
Stringname=redking;
};
classBextendsA
{
//The subclass defines a name property that is the same as in the parent class
Stringname=Michael;
publicvoidprint()
{
//Same as System.out.println(this.name);
System.out.println(name);
}
};
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
Bb=newB();
b.print();
}
};
The program prints the name attribute defined in the subclass: Michael
If we want to print the name attribute in the parent class, we can modify it to super.name
classA
{
Stringname=redking;
};
classBextendsA
{
//The subclass defines a name property that is the same as in the parent class
Stringname=Michael;
publicvoidprint()
{
//Same as System.out.println(this.name);
System.out.println(name);
//If we want to print the name attribute in the parent class, we can modify it to super.name
System.out.println(super.name);
}
};
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
Bb=newB();
b.print();
}
};
The name attribute in the parent class is output ha~ This is called attribute duplication
Attributes are usually required to be encapsulated. After being encapsulated, subclasses must not be able to see the content in the parent class, so basically they cannot be overwritten.
Can super and this call structure method be written together in a structure method? The answer is no~
When Super calls the structure method, it must be placed in the first line of the structure method, and this must also be placed in the first line when calling the structure method. If both are placed in the first line, they must conflict.
Some people think that the super method can not be called, let's see the following Demo02
classA
{
publicA(){}
};
classBextendsA
{
//There are three structure methods in it
publicB()
{
this(abc,888);
}
publicB(Stringname)
{
this();
}
publicB(Stringname,intage)
{
this(name);
}
};
This creates a problem, which we also mentioned when we talked about this keyword~
When using this() to call structure methods, you must leave an exit. Otherwise, the compilation will fail~
classA
{
publicA(){}
};
classBextendsA
{
//There are three structure methods in it
publicB()
{
//It's better to change this(abc,888); to super() as an export~
super();
}
publicB(Stringname)
{
this();
}
publicB(Stringname,intage)
{
this(name);
}
};
Now the compilation is passed~
Top knowledge points this season:
1. The class diagram of inheritance shows that
2. Inherited title
Now let's take a look at Demo03:
classA
{
};
classBextendsA
{
};
This drop inheritance contact us to show with a class diagram
Knowing the above, let's take a look at the following exercises:
classPerson
{
privateStringname;
privateStringaddr;
privatecharsex;
privateintage;
//Usually the structure method with few parameters is written on it~~
publicPerson(){}
publicPerson(Stringname, Stringaddr)
{
this.setName(name);
this.setAddr(addr);
this.setSex('male');
this.setAge(27);
}
publicPerson(Stringname,Stringaddr,charsex,intage)
{
this.setName(name);
this.setAddr(addr);
this.setSex(sex);
this.setAge(age);
}
publicvoidsetName(Stringname)
{
this.name=name;
}
publicvoidsetAddr(Stringaddr)
{
this.addr=addr;
}
//M: indicates male; F: indicates female
publicvoidsetSex(charsex)
{
this.sex=sex;
}
publicvoidsetAge(intage)
{
this.age=age;
}
publicStringgetName()
{
returnthis.name;
}
publicStringgetAddr()
{
returnthis.addr;
}
publicchargetSex()
{
returnthis.sex;
}
publicintgetAge()
{
returnthis.age;
}
//All content should be handed over to external output
publicStringgetInfo()
{
return name: this.name
,Address: this.addr
, Gender:  (this.sex=='M'? Male: Female)
, age:  this.age;
}
};
classStudentextendsPerson
{
privatefloatmath;
privatefloatenglish;
publicStudent()
{
// super() is implied by default;
super();
}
publicStudent(Stringname, Stringaddr)
{
super(name,addr);
}
publicStudent(Stringname,Stringaddr,charsex,intage,floatmath,floatenglish)
[Zero Basics JAVA] JavaSE Object-Oriented Part-14. Object-Oriented Advanced (02).doc)

[下载]08444527595.rar




上一篇:[零基础学JAVA]Java SE面向对象部分-13.面向对象高级(01)
下一篇:精通java