java的一些例题

本文通过六道编程题目展示了Java中多态、重载、重写、实例初始化和属性的应用,涉及类继承、方法覆盖及 polymorphism 的核心概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一题  属性与多态无关

package com.yan.test01;
public class Test01 {
        public static void main(String[] args) {
                A a = new B();
                System.out.println(a.num);
                System.out.println(((B)a).num);
                System.out.println(((A)((B)a)).num);
                System.out.println("-------------------");
                B b = new B();
                System.out.println(b.num);
                System.out.println(((A)b).num);
                System.out.println(((B)((A)b)).num);
        }
}
class A{
        int num = 1;
}
class B extends A{
        int num = 2;
}

第二题  实例初始化方法,属性与多态无关

package com.yan.test02;
public class Test02 {
        public static void main(String[] args) {
                Father f = new Son();
                System.out.println(f.x);
        }
}
class Father{
        int x = 10;
        public Father(){
                this.print();
                x = 20;
        }
        public void print(){
                System.out.println("Father.x = " + x);
        }
}
class Son extends Father{
        int x = 30;
        public Son(){
                this.print();
                x = 40;
        }
        public void print(){
                System.out.println("Son.x = " + x);
        }
}

第三题  多态,重写,实例初始化过程

package com.yan.test03;
public class Test03 {
        public static void main(String[] args) {
                Base b1 = new Base();
                Base b2 = new Sub();
        }
}
class Base {
        Base() {
                method(100);
        }
        public void method(int i) {
                System.out.println("base : " + i);
        }
}
class Sub extends Base {
        Sub() {
                super.method(70);
        }
        public void method(int j) {
                System.out.println("sub : " + j);
        }
}

第四题  多态、重载、重写

public class Test04 {
        public static void main(String[] args) {
                A a1 = new A();
                A a2 = new B();
                B b = new B();
                C c = new C();
                D d = new D();
                System.out.println("(1)" + a1.show(b));
                System.out.println("(2)" + a2.show(d));
                System.out.println("(3)" + b.show(c));
                System.out.println("(4)" + b.show(d));
        }
}
class A{
        public String show(D obj){
                return ("A and D");
        }
        public String show(A obj){
                return "A and A";
        }
}
class B extends A{
        public String show(B obj){
                return "B and B";
        }
        public String show(A obj){
                return "B and A";
        }
}
class C extends B{
}
class D extends B{
}

第五题  多态、重载、重写

public class Test05 {
        public static void main(String[] args) {
                A a1 = new A();
                A a2 = new B();
                B b = new B();
                C c = new C();
                D d = new D();
                System.out.println("(1)" + a1.show(b));
                System.out.println("(2)" + a2.show(d));
                System.out.println("(3)" + b.show(c));
                System.out.println("(4)" + b.show(d));
        }
}
class A {
        public String show(C obj) {
                return ("A and C");
        }
        public String show(A obj) {
                return "A and A";
        }
}
class B extends A {
        public String show(B obj) {
                return "B and B";
        }
        public String show(A obj) {
                return "B and A";
        }
}
class C extends B {
}
class D extends B {
}

第六题  属性与多态无关

public class Test06 {
        public static void main(String[] args) {
                Base b = new Sub();
                System.out.println(b.x);
        }
}
class Base{
        int x = 1;
}
class Sub extends Base{
        int x = 2;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值