通过链接构造函数来实例化对象

时间:2018-05-20 00:51:12

标签: java oop inheritance polymorphism

我的教科书在“文件和流”部分中有一个让我困惑的例子。

BufferedReader inFile = new BufferedReader (new FileReader ("data.txt"));

我的想法是创建一个BufferedReader类型的对象,并使用另一个类构造函数FileReader构建它们,然后将该对象“放置”到BufferedReader构造函数中。

为什么我们使用两个“新”关键字来实例化对象以及发生了什么?

这是属于多态还是继承?

2 个答案:

答案 0 :(得分:1)

也许这个等效代码会更有意义:

#include <stdio.h>
#include <string.h>
#define SIZE 4

typedef struct {
    int year;
    char model[11];
} Model;

typedef struct {
    float price;
} Price;

typedef struct {
    int miles;
} Miles;

typedef struct {
    Model year, model;
    Price price;
    Miles miles;
    struct Car *next;
} Car;

float averagePrice(Car *ptr_car, Car *ptr_car2, Car *ptr_car3, Car* ptr_car4);
int averageMiles(Car *ptr_car, Car *ptr_car2, Car *ptr_car3, Car *ptr_car4);

int main(void) {

    Car f1, im, f2, g;
    int i, cnt, avgmile;
    float avgprice;

    f1.year.year = 56;
    strcpy(f1.model.model, "Ford");
    f1.price.price = 500.00;
    f1.miles.miles = 23000;
    im.year.year = 64;
    strcpy(im.model.model, "Impala");
    im.price.price = 1800.00;
    im.miles.miles = 12000;
    f2.year.year = 57;
    strcpy(f2.model.model, "Ford");
    f2.price.price = 1400.00;
    f2.miles.miles = 22000;
    g.year.year = 65;
    strcpy(g.model.model, "Galaxy");
    g.price.price = 2600.00;
    g.miles.miles = 48000;
    avgprice = averagePrice(&f1, &im, &f2, &g);
    avgmile = averageMiles(&f1, &im, &f2, &g);
    printf("The average price of the vehicle is: %d.\n", avgprice);
    printf("The average miles of the vehicle is: %d.\n", avgmile);


    getchar();
    return 0;
}

float averagePrice(Car *ptr_car, Car *ptr_car2, Car *ptr_car3, Car *ptr_car4) {
    float total;

    total = ptr_car->price.price;
    total =+ ptr_car2->price.price;
    total =+ ptr_car3->price.price;
    total =+ ptr_car4->price.price;

    total = total / SIZE;

    return total;
}

int averageMiles(Car *ptr_car, Car *ptr_car2, Car *ptr_car3, Car* ptr_car4) {
    float total;

    total = ptr_car->miles.miles;
    total =+ ptr_car2->miles.miles;
    total =+ ptr_car3->miles.miles;
    total =+ ptr_car4->miles.miles;

    total = total / SIZE;

    return total;
}

所有这一切都构造了一个FileReader fileReader = new FileReader("data.txt"); BufferedReader inFile = new BufferedReader(fileReader); 对象,用作FileReader构造函数的参数。这是既不是多态也不是继承的例子,这只是在其他表达式中嵌套表达式。

答案 1 :(得分:1)

大多数流类可以链接在一起。 new运算符使用后面的构造函数返回以下类型的实例。因此,s1 = '''Tweet,Month,Day,Year Hello World,6,2,2013''' s2 = '''Month,Day,Year,Hour,Tweet January,2,2015,12,Happy New Year''' df1 = pd.read_csv(StringIO(s1)) df2 = pd.read_csv(StringIO(s2)) In []: from datetime import datetime df1['Date'] = pd.to_datetime(df1[['Year', 'Month', 'Day']]) df2['Month'] = df2['Month'].apply(lambda x: datetime.strptime(x, '%B').month) df2['Date'] = pd.to_datetime(df2[['Year', 'Month', 'Day']]) pd.concat([df1, df2])[['Date', 'Tweet']] Out[]: Date Tweet 0 2013-06-02 Hello World 0 2015-01-02 Happy New Year In []: df = pd.concat([df1, df2])[['Date', 'Tweet']].sort_values('Date', ascending=False) df['Date'] = df.Date.dt.strftime('%d-%b-%y')) df Out[]: Date Tweet 0 02-Jan-15 Happy New Year 0 02-Jun-13 Hello World 初始化为将被读取的文件,结果对象传递给FileReader,以便在实际读取期间缓冲文件中的读取以实现高效的I / O. / p>