「java继承ex」Java继承类调用父类方法

博主:adminadmin 2022-11-30 07:44:05 65

今天给各位分享java继承ex的知识,其中也会对Java继承类调用父类方法进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java程序中接口间能否有继承关系?

接口是可以被“继承的”(通常叫实现接口,继承父类),因为java是多继承单实现(可以继承多个接口,实现一个父类)的语言。

判断一个对象有继承关系,可以通过以下工具类来实现,返回true表示继承了:

public boolean isInterface(Class c, String szInterface)

{

Class[] face = c.getInterfaces();

for (int i = 0, j = face.length; i j; i++)

{

if(face[i].getName().equals(szInterface))

{

return true;

}

else

{

Class[] face1 = face[i].getInterfaces();

for(int x = 0; x face1.length; x++)

{

if(face1[x].getName().equals(szInterface))

{

return true;

}

else if(isInterface(face1[x], szInterface))

{

return true;

}

}

}

}

if (null != c.getSuperclass())

{

return isInterface(c.getSuperclass(), szInterface);

}

return false;

}

一道Java编程题 通过继承java.Util.Random类设计一个类RandomEX,并实现

Java程序:

import java.util.Random;

  

public class HardWork {

    public static void main(String[] args) {

     RandomEX rand = new RandomEX();

     int n = 4;

     int group = 10;

    

     for(int i=0; igroup; i++) {

     System.out.printf("第 %2d 组:\n", i + 1);

     System.out.printf("1~%d长度二进制字符串: \t%s\n", n, rand.nextBinaryString1(n));

     System.out.printf("固定%d长度二进制字符串: \t%s\n", n, rand.nextBinaryString2(n));

     System.out.printf("1~%d长度十六进制字符串:\t%s\n", n, rand.nextHexString1(n));

     System.out.printf("固定%d长度十六进制字符串:\t%s\n", n, rand.nextHexString2(n));

     System.out.println();

     }

    }

}

/**

 * 随机数类

 * @author 冯向科

 * @version 2016.05.07

 */

class RandomEX extends Random {

private static final long serialVersionUID = 1L;

/**

 * 产生随机长度为1~n的二进字符串

 * @param n 字符串最大长度

 * @return 长度为1~n的二进制字符串

 */

public String nextBinaryString1 (int n) {

long num;

StringBuilder str = new StringBuilder();

int len;

do {

num = this.nextLong();

str.append(Long.toBinaryString(num));

len = str.length();

} while(len  n);

int size = this.nextInt(n) + 1;

return str.substring(0, size);

}

/**

 * 产生固定长度为n的二进字符串

 * @param n 字符串固定长度

 * @return 固定长度为n的二进字符串

 */

public String nextBinaryString2 (int n) {

long num;

StringBuilder str = new StringBuilder();

int len;

do {

num = this.nextLong();

str.append(Long.toBinaryString(num));

len = str.length();

} while(len  n);

return str.substring(0, n).toUpperCase();

}

/**

 * 产生随机长度为1~n的十六进字符串

 * @param n 字符串最大长度

 * @return 长度为1~n的十六进制字符串

 */

public String nextHexString1 (int n) {

long num;

StringBuilder str = new StringBuilder();

int len;

do {

num = this.nextLong();

str.append(Long.toHexString(num));

len = str.length();

} while(len  n);

int size = this.nextInt(n) + 1;

return str.substring(0, size).toUpperCase();

}

/**

 * 产生固定长度为n的十六进字符串

 * @param n 字符串固定长度

 * @return 固定长度为n的十六进字符串

 */

public String nextHexString2 (int n) {

long num;

StringBuilder str = new StringBuilder();

int len;

do {

num = this.nextLong();

str.append(Long.toHexString(num));

len = str.length();

} while(len  n);

return str.substring(0, n).toUpperCase();

}

}

运行测试:

第  1 组:

1~4长度二进制字符串: 11

固定4长度二进制字符串: 1010

1~4长度十六进制字符串: 44B

固定4长度十六进制字符串: A7C3

第  2 组:

1~4长度二进制字符串: 110

固定4长度二进制字符串: 1110

1~4长度十六进制字符串: E96D

固定4长度十六进制字符串: 61F1

第  3 组:

1~4长度二进制字符串: 101

固定4长度二进制字符串: 1111

1~4长度十六进制字符串: B1C

固定4长度十六进制字符串: A0F1

第  4 组:

1~4长度二进制字符串: 1101

固定4长度二进制字符串: 1010

1~4长度十六进制字符串: 1DC

固定4长度十六进制字符串: D38E

第  5 组:

1~4长度二进制字符串: 1

固定4长度二进制字符串: 1110

1~4长度十六进制字符串: 70D8

固定4长度十六进制字符串: 495B

第  6 组:

1~4长度二进制字符串: 1100

固定4长度二进制字符串: 1011

1~4长度十六进制字符串: F6

固定4长度十六进制字符串: 5086

第  7 组:

1~4长度二进制字符串: 100

固定4长度二进制字符串: 1100

1~4长度十六进制字符串: 86A

固定4长度十六进制字符串: 23A0

第  8 组:

1~4长度二进制字符串: 1

固定4长度二进制字符串: 1101

1~4长度十六进制字符串: 8

固定4长度十六进制字符串: F967

第  9 组:

1~4长度二进制字符串: 110

固定4长度二进制字符串: 1000

1~4长度十六进制字符串: F194

固定4长度十六进制字符串: 7C3D

第 10 组:

1~4长度二进制字符串: 1101

固定4长度二进制字符串: 1100

1~4长度十六进制字符串: 4

固定4长度十六进制字符串: FB2A

java中继承的语法格式是怎样的

//父类 Shape类的声明

abstract class Shape

{

public final double PI = 3.141592654;

protected Color color; //颜色

protected int lineSize; //线宽

public Shape() //构造函数1

{

color = Color.black;

lineSize = 1;

}

public Shape(Color c, int ls) //构造函数2

{

color = c;

lineSize = ls;

}

//设置颜色方法

public void setColor(Color c)

{

color = c;

}

//获得颜色方法

public Color getColor()

{

return color;

}

//设置线宽方法

public void setLineSize(int ls)

{

lineSize = ls;

}

//获得线宽方法

public int getLineSize()

{

return lineSize;

}

//抽象方法area

abstract double area();

//抽象方法perimeter

abstract double perimeter();

}

//子类Circle的声明

class Circle extends Shape

{

int centerX; //圆心X坐标

int centerY; //圆心Y坐标

int radius; //圆的半? //构造函数

public Circle(int x, int y, int r)

{

super(); //使用父类的构造函数构建父类

centerX = x;

centerY = y;

radius = r;

}

public double area()

{

return (int)(PI * radius * radius );

}

public double perimeter()

{

return (int)(2*PI*radius);

}

}

//子类Rectangle声明

class Rectangle extends Shape

{

int left; //矩形左上角X坐标

int top; //矩形左上角Y坐标

int width; //矩形长度

int height; //矩形宽度

//构造函数

public Rectangle(int l, int t, int w, int h)

{

super();

left = l;

top = t;

width = w;

height = h;

}

public double area()

{

return (int)(width * height);

}

public double perimeter()

{

return (int)((width + height)*2);

}

}

//子类RectangleEx声明

class RectangleEx extends Rectangle

{

int radius; // 圆角半径?

//构造函数

public RectangleEx(int l, int t, int w, int h, int r)

{

super(l, t, w, h); //用父类构造函数构建父类

radius = r;

}

public double area()

{

return (int)(super.area() - (4 - PI) * radius* radius);

}

public double perimeter()

{

return (int)((width + height)*2-8*radius+2*PI*radius);

}

}

public class extend

{

public static void main(String args[])

{

//创建三个Shape对象

Shape shape[] = new Shape[3];

shape[0] = new Circle(50, 50, 40);

shape[1] = new Rectangle(0, 0, 40, 30);

shape[2] = new RectangleEx(20, 30, 40, 30, 5);

//创建三个Shape对象的信息数组

String ShapeName[]=new String[3];

ShapeName[0]="圆Circle(50, 50, 40)";

ShapeName[1]="矩形Rectangle(0, 0, 40, 30)";

ShapeName[2]="圆角矩形RectangleEx(20, 30, 40, 30, 5)";

//求面积和周长

for(int i = 0; i shape.length; i++)

{

System.out.println(ShapeName[i]+"的面积="+ shape[i].area());

System.out.println(ShapeName[i]+"的周长="+shape[i].perimeter());

System.out.println();

}

}

}

一道Java编程题,通过继承java.util.Random类设计一个类RandomEx,并实现以下功能

import java.util.Random;

public class RandomEx extends Random{

public  char nextChar(char x){

Random r = new Random();

if((int)x = 48(int)x=57)

return (char) (r.nextInt(9)+48);

else if(x=65x=90)

return (char)(r.nextInt(25)+65);

else if(x=97x=122)

return (char)(r.nextInt(25)+97);

else

return '?';

}

public String toString(){

return "RandomEx";

}

}

import java.util.Random;

public class TestR {

public static void main(String[] args) {

Random r = new Random();

RandomEx ra = new RandomEx();

for(int j = 0;j  2;++j)

for(int i = 48;i = 57;++i)

System.out.print(ra.nextChar((char)i)+" ");

System.out.println();

int i = 0;

char x = 'a';

while(i != 20){

System.out.print(ra.nextChar(x++) + " ");

i++;

}

System.out.println();

int j = 0;

char y = 'A';

while(j != 20){

System.out.print(ra.nextChar(y++) + " ");

j++;

}

System.out.println();

System.out.println(ra.toString());

}

}

java继承ex的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Java继承类调用父类方法、java继承ex的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-30,除非注明,否则均为首码项目网原创文章,转载请注明出处。