第十五天(多态)

本文探讨了C++中的多态特性,通过实例展示了如何实现多态功能,并详细解释了四种不同的权限修饰符(public, private, protected)的作用域。通过具体的代码验证,帮助读者深入理解这些概念。" 136985675,17243493,深度学习中的半监督微调技术实践,"['深度学习', '神经网络', '迁移学习', '半监督学习', '大数据']
1(多态)有如下代码
class Super{
public void m(){
foo();
}
public void foo(){
System.out.println("foo() inSuper");
}
}
class Sub extends Super{
public void foo(){
System.out.println("foo() inSub");
}
}
public class TestSuperSub{
public static void main(String args[]){
Super s = new Sub();
s.m();
}
}
选择正确答案
A. 程序编译不通过
B. 编译通过,输出foo() in Super

C. 编译通过,输出foo() in Sub


C


2. *(多态,instanceof)有如下代码
class Animal{
private String name;
// 1
}
class Dog extends Animal{
//2
}
class Cat extends Animal{
//3
}
public class TestAnimal{
public static void main(String args[]){
Animal[] as = new Animal[]{
new Dog("Pluto"),
new Cat("Tom"),
new Dog("Snoopy"),
new Cat("Garfield")
};
Dog[] dogs = getAllDog(as);
for(int i = 0; i<=dogs.length; i++){
System.out.println(dogs.getName());
}
}
public static Dog[] getAllDog(Animal[] as){
//4
}
}
程序填空:
a) //1, //2, //3 处填上适当的get/set 方法和构造方法
b) 完成//4 处的填空。getAllDog 方法从一个Animal 数组中挑选出所有的Dog 对象,并把这些对象放在一个Dog 数组中返回。
class Animal {
        private String name;
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
        public Animal(String name) {
                super();
                this.name = name;
        }
}
 
class Dog extends Animal {
        public Dog(String name) {
                super(name);
        }
}
 
class Cat extends Animal {
        public Cat(String name) {
                super(name);
        }
}
 
public class TestAnimal {
        public static void main(String args[]) {
                Animal[] as = new Animal[] { new Dog("Pluto"), new Cat("Tom"), new Dog("Snoopy"), new Cat("Garfield") };
                Dog[] dogs = getAllDog(as);
                for (int i = 0; i < dogs.length; i++) {
                        if(dogs!=null){                            
                                System.out.println(dogs.getName());
                        }
                }
        }
 
        public static Dog[] getAllDog(Animal[] as) {
                // getAllDog 方法从一个Animal 数组中挑选出所有的Dog 对象,
            //   并把这些对象放在一个Dog 数组中返回。           
                Dog[] dog = new Dog[as.length];
                int j = 0;
                for (int i = 0; i < as.length; i++) {
                        if (as[i] instanceof Dog) {
                                dog[j++] = (Dog) as[i];
                        }
                }[i][i]
                return dog;
        }
}

3. *(多态)写一个函数getShape(int i),获取相关图形的面积,该函数的参数为一个整数i,返回值由决定:
a) i == 0 时,返回一个半径为的圆形
b) i == 1 时,返回一个长为宽为的矩形
c) i == 2 时,返回一个边长为的正方形
//图形
class Graphical {
        public Object getShape() {
                return null;
        }
}
 
//圆形
 
class Circular extends Graphical {
        public Object getShape() {
                return Math.PI * 1 * 1;
        }
}
 
//矩形
 
class Rectangle extends Graphical {
        public Object getShape() {
                return 3 * 2;
        }
}
 
//正方形
 
class Square extends Graphical {
        public Object getShape() {
                return 2 * 2;
        }
}
 
 
 
public class Demo03 {
        public static void main(String args[]) {
                Demo03 d = new Demo03();
                System.out.println("请输入一个整数:(范围:0-2)");
                Scanner sc = new Scanner(System.in);
                int input = sc.nextInt();
                d.getShape(input);
        }
 
        public void getShape(int i) {
                if (i == 0) {
                        Graphical g = new Circular();
                        Object o = g.getShape();
                        System.out.println(o);
                } else if (i == 1) {
                        Graphical g1 = new Rectangle();
                        Object o = g1.getShape();
                        System.out.println(o);
                } else if (i == 2) {
                        Graphical g2 = new Square();
                        Object o = g2.getShape();
                        System.out.println(o);
                }
        }
}

4. (多态)写一个函数,接受一个图形(长方形,正方形,圆)作为参数,打印出该图形的周长和面积

public  class TestGraphics{        
        public static void main(String[] args) {
                Graphics g1 = new Rectangle();
                g1.perimeter_area(new Rectangle(3,5));
                Graphics g2 = new Square();
                g2.perimeter_area(new Square(5));
                Graphics g3 = new Circle();
                g3.perimeter_area(new Circle(3));
        }
}
 
 
/**
 
 * 图形类
 
 */
 
class Graphics{
        private double length;//长度(或是圆的半径,或是正方形的边长)
        private double width;//宽度
        public static double PI = Math.PI;
        public Graphics() {}
        public Graphics(double length) {
                this.length = length;
        }
        public Graphics(double length, double width) {
                this.length = length;
                this.width = width;
        }
 
        public double getLength() {
                return length;
        }
 
        public void setLength(double length) {
                this.length = length;
        }
 
        public double getWidth() {
                return width;
        }
 
        public void setWidth(double width) {
                this.width = width;
        }
        public void perimeter_area(Graphics g){}
}
 
 
 
/**
 * 
 * 长方形类
 *
 */
 
class Rectangle extends Graphics{
        private String name = "长方形";
        public Rectangle(){
        }
 
        public Rectangle(double length, double width){
                super(length, width);
        }
        
        @Override
        public void perimeter_area(Graphics g){
                System.out.print(name+"的周长是:"+((g.getLength()+g.getWidth())*2)+"\t");
                System.out.println(name+"的面积是:"+(g.getLength()*g.getWidth()));
        }
}
 
 
 
/**
 * 
 * 正方形类
 *
 */
 
class Square extends Graphics{
 
        private String name = "正方形";       
        public Square(){               
        }
 
        public Square(double length){
                super(length);
        }
       
        @Override
        public void perimeter_area(Graphics g){
                System.out.print(name+"的周长是:"+(g.getLength()*4)+"\t");
                System.out.println(name+"的面积是:"+Math.pow(g.getLength(), 2));
        }
}
 
 
 
/**
 * 
 * 圆形类
 *
 */
 
class Circle extends Graphics{
 
        private String name = "圆形";
        public Circle(){                
        }
 
        public Circle(double length){
                super(length);
        }
     
        @Override
        public void perimeter_area(Graphics g){
                System.out.print(name+"的周长是:"+2*g.PI*g.getLength()+"\t");
                System.out.println(name+"的面积是:"+g.PI*Math.pow(g.getLength(), 2));
        }
}


5 **(继承、访问修饰符)有如下代码
//MySuperClass.java
package corejava.chp6;
class MySuperClass{
protected int value;
}
//TestMain.java
packagecorejava.temp;
import corejava.chp6.*;
class MySubClass extends MySuperClass{
public void print(){
System.out.println(value);
}
}
publicclass TestMain{
public static void main(String args[]){
MySubClass msc = new MySubClass();
msc.print();
System.out.println(msc.value);
}
}
这段代码能否编译通过?如果可以,输出结果是什么?如果不能,原因是什么?
编译不能通过,理由:仅限本类,不同包子类和同包类可以访问,不同包的其他类不可以访问


6、总结4种权限修饰符作用域、举例验证

/**
 
 * 6、总结4种权限修饰符作用域、举例验证
 
 * 作用域:
 
         *                                 本类                同包类            子类                不同包其他类
 
                public                 可用                     可用                可用                可用
 
                protected               可用                   可用                可用                不可用
 
                (default)             可用                   可用                不可用             不可用
 
                private             可用                  不可用              不可用             不可用
 
 */       
 
         
 
public class Demo06 {
        private String name = "张三";
        public static void main(String[] args) {
                //调用本类中的属性
                Demo06 d = new Demo06();
                System.out.println(d.name);              
                //调用相同包不同类的属性
                Test test = new Test();
                System.out.println(test.name);               
                //调用不同包子类中的方法
              Other o = new Other();
                o.test();            
                //调用受保护的方法
                Other other = new Other();
                other.test1();               
                //调用不同包中其他类的方法
                Demo demo = new Demo();
                demo.test();
        }
}
 
class Test{
        String name = "李四";
}
 
/*============华丽丽的分割线==========*/
 
package com.lemon.object;
 
public class Other extends Demo06{        
        public void test(){
                System.out.println("我是Demo06的子类中的方法");
        }
        protected void test1(){
                System.out.println("我是受保护的方法");
        }
}
 
/*============华丽丽的分割线==========*/
 
package com.lemon.test;
 
public class Demo {
        public void test(){
                System.out.println("我是不同包中其他类的方法");
        }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值