Java-面向对象练习

发布于:2023-01-19 ⋅ 阅读:(368) ⋅ 点赞:(0)

1.看程序写结果

1.1 下面Java代码的运行结果是
public class Dog {
String name=null;
int age = 10;
public Dog() {
this.name = “旺财”;
this.age = 20;
}
public void show(){
System.out.println(“名字:”+name+" 年龄:"+age);
}
public static void main(String[] args) {
new Dog().show();
}
}
答案:名字:旺财 年龄:20

1.2在Java中,以下程序编译运行后的输出结果
public class Test {
int x = 2;
int y = 4;
Test (int x, int y) {
this.x = x;
this.y = y;
}
public static void main(string[] args) {
Test ptl = new Test (3, 3);
Test pt2 = new Test (4, 4);
System. out. print (pt1.x + pt2.x);
}
}
答案:7

1.3写出结果。
public class Test {
int i = 0;
void change(int i){
i++;
System.out.println(i);
}
void change1(Test t){
t.i++;
System.out.println(t.i);
}
public static void main(String[] args) {
Test ta = new Test();
System.out.println(ta.i);
ta.change(0);
System.out.println(ta.i);
ta.change1(ta);
System.out.println(ta.i);
}
}}}
答案:
0
1
0
1
1

1.4写出结果
public class Demo{
public static void main(String[] args){
int[] a=new int[1];
modify(a);
System.out.println(a[0]);
}
public static void modify(int[] a){
a[0]++;
}
}
答案:1

1.5写出结果
public class TestArgsValue {
public static void main(String[] args) {
int i = 10;
TestArgsValue tv = new TestArgsValue();
tv.method1(i);
System.out.println(“i=” + i);
System.out.println();
Demo d = new Demo();
System.out.println(d);
tv.method2(d);
System.out.println("d.i = " + d.i);
}
public void method1(int i){
System.out.println(“i=” + i++);
}
public void method2(Demo d){
System.out.println(d);
System.out.println("d.i : " + d.i++);
}
}
class Demo{
int i = 5;
}
答案:
i=10
i=10
com.java2022_8_2.javaDemo.Demo@1b6d3586
com.java2022_8_2.javaDemo.Demo@1b6d3586
d.i : 5
d.i = 6

1.6写出结果
public class Test{
public static void leftshift(int i, int j){
i+=j;
}
public static void main(String args[]){
int i = 4;
int j = 2;
leftshift(i, j);
System.out.println(i);
}
}
答案:4

1.7
哪个选项和show函数重载
class Demo{
void show(int a,int b,float c){ }
}
A.void show(int a,float c,int b){ }
B,void show(int a,int b,float c){ }
C.int show(int a,float c,int b){ return a; }
D.int show(int a,float c){ return a; }
答案: A C D

1.8
与void show(int a,char b,double c){}构成重载的有:
a)void show(int x,char y,double z){ }
b)int show(int a,double c,char b){ }
c) void show(int a,double c,char b){ }
d) boolean show(int c,char b){ }
e) void show(double c){ }
f) double show(int x,char y,double z){ }
g) void shows(){ double c }
答案:b) c) d) e) g)

1.9public class Test {
Person person = new Person(“Test”);
int age;
static{
System.out.println(“test static”);
}
{
System.out.println(“test”);
}
public Test() {
System.out.println(“test constructor”);
}
public static void main(String[] args) {
new Test();
}
}
class Person{
static{
System.out.println(“person static”);
}
public Person(String str) {
System.out.println("person "+str);
}
}
答案:提示:执行顺序(成员变量,实例代码块,构造方法,成员方法)
类的元素加载顺序(静态 非静态)
test static
person static
person Test
test
test constructor

2.概述题

2-1 静态成员变量和非静态成员变量的区别
答:静态的成员变量可以使用类名直接调用,非静态成员变量必须通过对象调用;静态成员变量可以被所有对象共享,也就是说对类的所有对象都有影响,非静态成员变量只对一个对象有影响;静态成员变量存储在方法区,非静态成员变量存储在堆内存中;静态成员变量随着类的加载而加载,类加载完成,就分配完空间,优先于对象存在,非静态成员变量创建对象的时候分配空间


网站公告

今日签到

点亮在社区的每一天
去签到