重载:就好比
public class CatOverLoad {
public void cat(){
System.out.println("重载cat方法开始");
}
public void cat(String name){
System.out.println("重载"+name+"方法开始");
}
public String cat(String name,int age){
return name+age;
}
public String cat(String name,int age,String type){
return type;
}
public String cat(int age,String name){
return age+name;
}
}
overload 就是调用时,根据不同的参数去调用方法
在JDK1.5之后映入了可变个数形参
public void cat(String ... str){
System.out.println(str);
}
this 的指向当前对象
public class Phone {
public static void main(String[] args) {
Phone phone = new Phone();
}
private int age;
private String name;
Phone(){
System.out.println("无参");
}
Phone(int age,String name){
System.out.println("全参");
}
public void setAge(int age){
this.age=age;
}
}
继承
1 当子类重写父类方法时:调用会执行子类方法
2 用static 修饰符修饰时,调用用类名直接可以,staic final 修饰的也一样
3 继承实例化时,先父类
4父类方法用final 和普通修饰都一样,都可以通过子类调用
package test6.extend;
import test6.Phone;
public class PhoneTest {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
PhoneTest(){
this.finalMethod();
System.out.println("Phoen无参构造");
}
PhoneTest(String name,int age){
System.out.println(name+age);
}
public void method(){
System.out.println("PhoneTestMethod");
}
public static final void staticFinalMethod(){
System.out.println("final+static修饰的方法");
}
public static void staticMethod(){
System.out.println("static修饰的方法");
}
public final void finalMethod(){
System.out.println("final修饰的方法");
}
}
package test6.extend;
import org.jcp.xml.dsig.internal.SignerOutputStream;
import test6.Phone;
public class SonTest extends PhoneTest {
private char sex;
private String name;
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
}
SonTest(){
PhoneTest.staticFinalMethod();
PhoneTest.staticMethod();
super.finalMethod();
System.out.println("Son无参构造");
}
SonTest(char sex,String name){
System.out.println("全参构造");
}
public void method(){
System.out.println("SonTestMethod=======================>son");
}
}
package test6.extend;
public class Method {
public static void main(String[] args) {
SonTest sonTest = new SonTest();
sonTest.method();
sonTest.finalMethod();
}
}
输出结果:
— Person p=new Man();`
public class Person {
String name;
int age;
Person(){
System.out.println("Person无参构造====================>");
}
Person(String name,int age){
System.out.println(name+age);
}
public void method(){
System.out.println("这是一个Person的方法==========''");
}
public void method(String name){
System.out.println("这是一个Person的方法=========='name'");
}
public void method(String name,int age){
System.out.println("这是一个Person的方法=========='name+age'");
}
public static void staticMethod(){
System.out.println("这是一个Person的方法=========='staticMethod'");
}
public final void finalMethod(){
System.out.println("这是一个Person的方法=========='staticMethod");
}
}
package test6.polymorphic;
public class PoiyMan extends Person {
String name;
int age;
PoiyMan(){
System.out.println("PoiyMan的无参构造======》");
}
PoiyMan(String name,int age){
System.out.println("PoiyMan的全参构造=======》");
}
public void method(){
System.out.println("这是一个PoiyMan的方法==========''");
}
public void method(String name){
System.out.println("这是一个PoiyMan的方法=========='name'");
}
public void method(String name,int age){
System.out.println("这是一个PoiyMan的方法=========='name+age'");
}
public static void staticPoiyMan(){
System.out.println("这是一个PoiyMan的方法=========='staticMethod'");
}
public final void finalPoiyMan(){
System.out.println("这是一个PoiyMan的方法=========='staticPoiyMan");
}
}
package test6.polymorphic;
public class PolyMain {
public static void main(String[] args) {
Person person = new PoiyMan();
person.method();
person.method("aike");
person.method("aike",23);
person.finalMethod();
}
}
1 方法初始化时 先将父类实例化
2 重载是在一个类中,而重写是在至少俩个类中
3 谁的方法看左边,真正运行看右边,能执行左边特有的方法,不能执行右边特有的方法
4父类调用时,子类会根据不同的实例去重写不同实例中的方法
public class Animal {
public void eat(){
System.out.println("动物吃东西");
}
}
public class Cat extends Animal{
public void eat(){
System.out.println("猫吃");
}
}
public class Dog extends Animal{
public void eat(){
System.out.println("狗吃肉");
}
}
public class AnimalMain {
public static void main(String[] args) {
Animal animal=new Cat();
animal.eat();
Animal animal1=new Dog();
animal1.eat();
}
}
instanceof
a instanceof A 判断a是否是A类的实例



