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,返回值由i 决定:
a)
i == 0 时,返回一个半径为1 的圆形
b)
i == 1 时,返回一个长为3 宽为2 的矩形
c)
i == 2 时,返回一个边长为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("我是不同包中其他类的方法");
}
}
本文探讨了C++中的多态特性,通过实例展示了如何实现多态功能,并详细解释了四种不同的权限修饰符(public, private, protected)的作用域。通过具体的代码验证,帮助读者深入理解这些概念。"
136985675,17243493,深度学习中的半监督微调技术实践,"['深度学习', '神经网络', '迁移学习', '半监督学习', '大数据']
&spm=1001.2101.3001.5002&articleId=72857040&d=1&t=3&u=1a3bab311c58460bb10dcb36ecee367f)
784

被折叠的 条评论
为什么被折叠?



