我正在研究java中的平均值,中位数。我创建了一个程序,但我无法在其上正确实现继承。在我的代码中,对于每个继承的类对象,它的请求输入。
我希望输入只需要一次,然后使用继承均值和模式进行计算。请建议。
import java.io.*;
import java.util.*;
class A
{
int a[];
int no;
A()
{
System.out.println("Number of elements for an array : ");
Scanner in1 = new Scanner(System.in);
no = in1.nextInt();
a = new int[no];
for(int i=0; i<no; i++)
{
System.out.println("Enter elements : ");
Scanner in2 = new Scanner(System.in);
a[i] = in2.nextInt();
}
}
void sort()
{
int tmp;
for(int i=0; i<no-1; i++)
{
for(int j=i+1; j<no; j++)
{
System.out.println("a[i] : " + a[i] + " a[i+1] : " + a[i+1]);
if (a[i]>a[j])
{
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
}
void display()
{
System.out.println();
for(int i=0; i<no; i++)
{
System.out.println(a[i]);
}
}
}
class B extends A
{
int tot = 0;
float mean;
void get_mean()
{
System.out.println("no : " + no);
for(int i=0; i<no; i++)
{
tot += a[i];
}
System.out.println("tot : " + tot);
mean = (float) tot/no;
System.out.println("mean : " + mean);
}
}
class C extends A
{
void median()
{
float median;
if(no%2==0)
{
int middle = no/2;
median = (float)((a[middle] + a[middle/2])/2);
}
else
{
median = a[(no/2)];
}
System.out.println("median : " + median);
}
}
class test
{
public static void main (String arg[]) throws IOException
{
B obj1 = new B();
obj1.get_mean();
C obj2 = new C();
obj2.sort();
obj2.median();
}
}
答案 0 :(得分:1)
继承为您提供通用API和公共基本类型而不是数据。
添加复制构造函数,该构造函数接受“A”参数,然后从传递的“A”对象复制属性。像这样:
package com.example;
import java.io.IOException;
import java.util.Scanner;
class A {
int a[];
int no;
A() {
}
void init() {
System.out.println("Number of elements for an array : ");
Scanner in1 = new Scanner(System.in);
no = in1.nextInt();
a = new int[no];
for (int i = 0; i < no; i++) {
System.out.println("Enter elements : ");
Scanner in2 = new Scanner(System.in);
a[i] = in2.nextInt();
}
}
void sort() {
int tmp;
for (int i = 0; i < no - 1; i++) {
for (int j = i + 1; j < no; j++) {
System.out.println("a[i] : " + a[i] + " a[i+1] : " + a[i + 1]);
if (a[i] > a[j]) {
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
}
void display() {
System.out.println();
for (int i = 0; i < no; i++) {
System.out.println(a[i]);
}
}
}
class B extends A {
B(A obj) {
no = obj.no;
a = new int[obj.a.length];
System.arraycopy( obj.a, 0, a, 0, obj.a.length );
}
int tot = 0;
float mean;
void get_mean() {
System.out.println("no : " + no);
for (int i = 0; i < no; i++) {
tot += a[i];
}
System.out.println("tot : " + tot);
mean = (float) tot / no;
System.out.println("mean : " + mean);
}
}
class C extends A {
C(A obj) {
no = obj.no;
a = new int[obj.a.length];
System.arraycopy( obj.a, 0, a, 0, obj.a.length );
}
void median() {
float median;
if (no % 2 == 0) {
int middle = no / 2;
median = ((a[middle] + a[middle / 2]) / 2);
} else {
median = a[(no / 2)];
}
System.out.println("median : " + median);
}
}
public class Test {
public static void main(String arg[]) throws IOException {
A objA = new A();
objA.init();
B obj1 = new B(objA);
obj1.get_mean();
C obj2 = new C(objA);
obj2.sort();
obj2.median();
}
}
但更好的方法是使用组合而不是继承。 让你的A类收集数据然后将这个“数据”对象注入B,C。 B和C不需要是“IS-A”对象。它们应该是“HAS-A”对象。