
- public class TestConflict {
- public static void main(String[] args) {
- Son s = new Son();
- s.play();
- }
- }
- //接口
- interface Friend{
- default void play(){
- System.out.println("跟朋友出去玩唱ktv");
- }
- }
- //父类
- class Father{
- public void play(){
- System.out.println("跟父亲出去钓鱼");
- }
- }
- class Son extends Father implements Friend {
- // 1.如果不重写冲突的方法,那么会保留父类的
- @Override
- public void play() {
- // 2.重写调用父类
- // super.play();
- // 3.父接口
- Friend.super.play();
- // 4.自己完全重写
- System.out.println("自己出去玩或者跟着momoko学java");
- }
- }
- public class TestConflict {
- public static void main(String[] args) {
- Girl girl = new Girl();
- girl.play();
- }
- }
- //接口1
- interface Friend{
- default void play(){
- System.out.println("跟朋友出去玩唱ktv");
- }
- }
- //接口2
- interface BoyFriend{
- default void play(){
- System.out.println("跟男朋友去约会");
- }
- }
- class Girl implements Friend,BoyFriend{
- @Override
- public void play() {
- // 1.保留1接口
- Friend.super.play();
- // 2.保留2接口
- BoyFriend.super.play();
- // 3.完全重写
- System.out.println("自己出去玩");
- }
- }
- public class TestConflict {
- public static void main(String[] args) {
- Girl girl =new Girl();
- girl.play();
- }
- }
- //接口1
- interface Friend{
- default void play(){
- System.out.println("跟朋友出去玩唱ktv");
- }
- }
- //接口2
- interface BoyFriend{
- default void play(){
- System.out.println("跟男朋友去约会");
- }
- }
-
- interface superFriend extends Friend,BoyFriend{
- // 接口解决冲突是要加上default
- @Override
- default void play() {
- Friend.super.play();
- }
- }
- class Girl implements superFriend{
- @Override
- public void play() {
- // superFriend.super.play();
- System.out.println("我自己再重写");
- }
- }
- public class TestSub {
- public static void main(String[] args) {
- SubClass sub = new SubClass();
- sub.method();
- }
-
-
- }
- class SubClass extends SuperClass implements SuperInterface,FatherInterface{
- public void method(){
- // 不清楚,你自己抉择->写清楚
- // System.out.println(x);
- // 通过父类.量名
- System.out.println("SuperClass的x"+ super.x);
- // 通过接口名.常量名
- System.out.println("SuperInterface的x"+ SuperInterface.x);
- System.out.println("FatherInterface的x"+ FatherInterface.x);
- }
- }
- //类
- class SuperClass{
- int x = 1;
- }
- //接口1
- interface SuperInterface{
- // 其中public static final可以省略
- int x = 2;
- }
- //接口2
- interface FatherInterface{
- // 其中public static final可以省略
- int x = 3;
- }
当类在继承父类和实现接口中出现冲突的情况
*方法冲突:
* 1)父类和父接口中的方法名发生冲突 解决方案:默认亲爹优先 父类:super.方法名 父接口名.super.方法名
* 2)实现多个接口中的方法名发生冲突 必须抉择(不然报错) 父接口名.super.方法名
* 3)父接口继承了多个接口 解决方案:父接口解决冲突 子类中进行重写
* 4)父类和父接口的量发生冲突 必须抉择(不然报错) 父类:super.量名 父接口:父接口名.常量名