public class testclass { public static void main(String[] args){ Student stu1 = new Student(); Comp c1 = new Comp(); c1.band="梁河县"; stu1.comp = c1; c1.band="1111"; System.out.println(stu1.comp.band); } } class Comp { String band; } class Student { int a; int b; Comp comp; }
1111
每个类的对象都需要实例化,允许对象赋值指向同一实例化内容。所以改变例子中c1的值实际也改变了stu1.comp.band的值。
时间: 2024-12-28 05:24:13