在这个Object Relation的代码片段中,我在Class One中引用了Class Two,并通过它访问了Class Two的成员。
如果我在Class One中创建Class Two的Object然后访问它的方法怎么办?这样做会有什么不同?
我知道我在这里错过了一些概念,但我没有得到它。
// Object Relation Using References
package Object_Relation;
class One {
// Instance Variables
int x;
Two t;
public One(Two t) {
this.t = t;
x=10;
}
void display() {
System.out.println("Class One Members : ");
System.out.println("x = "+x);
System.out.println("Displaying Class Two Members using its Method");
t.display();
System.out.println("Displaying Class Two Members using its reference :");
System.out.println("y = "+t.y);
}
}
class Two {
// Instance Variables
int y;
public Two(int y) {
this.y = y;
}
public void display() {
System.out.println("y = "+y);
}
}
public class UsingReference {
public static void main(String[] args) {
Two t2 = new Two(20);
One o = new One(t2);
o.display();
}
}
答案 0 :(得分:0)
我不确定究竟是什么问题?
但是在示例中,One和Two都是不同的类。并且为了访问非静态成员变量,您需要一个类Two的对象。
您是否在寻找关于java中内部类的一些概念: - 实施例
if(chart=='stacked_chart'){
console.log(JSON.stringify(response));
var series = [{name:'Coached',data:[]},{name:'Non Coached',data:[],stack: 'yes'},{name:'Delta',data:[],stack: 'yes'}],
year, month, day;
for(var i=0; i<response.length;i++){
var d = response[i].date.split(",");
year = parseFloat(d[0]);
month = parseFloat(d[1])-1; //date.utc method starts month 0-11
day = parseFloat(d[2]);
var coached = +(parseFloat(response[i].coached).toFixed(3));
var non_coached = +(parseFloat (response[i].non_coached).toFixed(3));
var delta = +(parseFloat (response[i].delta).toFixed(3));
series[0].data.push([Date.UTC(year,month,day),coached]);
series[1].data.push([Date.UTC(year,month,day),non_coached]);
series[2].data.push([Date.UTC(year,month,day),delta]);
console.log(Date.UTC(year,month,day));
}
console.log(series);
if(series.length){
stacked_chart.userOptions.series = series;
var chart = new Highcharts.Chart(stacked_chart.userOptions);
console.log(chart.series[0].data[0].options);
console.log(chart.series[1].data[0].options);
console.log(chart.series[2].data[0].options);
}
}
PS:这里InnerClass是OuterClass的成员。