下面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
在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
写出结果
public class Test {
int i = 0;
void change(int i){
i++;
System.out.println(i);//1
}
void change1(Test t){
t.i++;
System.out.println(t.i);//1
}
public static void main(String[] args) {
Test ta = new Test();
System.out.println(ta.i);//0
ta.change(0);
System.out.println(ta.i); //0
ta.change1(ta);
System.out.println(ta.i);//1
}
}
//01011
写出结果
public class TestArgsValue {
public static void main(String[] args) {
int i = 10;
TestArgsValue tv = new TestArgsValue();
tv.method1(i);
System.out.println("i=" + i);//i=10
System.out.println();//
Demo d = new Demo();
System.out.println(d);//地址
tv.method2(d);
System.out.println("d.i = " + d.i);//d.i=6
}
public void method1(int i){
System.out.println("i=" + i++);//i=10
}
public void method2(Demo d){
System.out.println(d);//地址
System.out.println("d.i : " + d.i++);//d.i:5
}
}
class Demo{
int i = 5;
}
写出结果
/*
加载一次实例块,一次静态代码块
静态代码块优先于对象,所以先输出
*/
public 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
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
}
}
//4
哪个选项和show()重载 A
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; }
编程 比较大小:分别利用重载,对两个int,两个double,三个int,三个double类型的数进行比较大小,返回其中较大的一个.
package Day04.HM;
/*
比较大小:分别利用重载,对两个int,两个double,三个int,三个double类型的数进行比较大小,返回其中较大的一个.
*/
public class Compare {
void Com(int a,int b){
System.out.println(a>b?a:b);
}
void Com(int a,int b,int c){
if(a>b){
if(c>a){
System.out.println(c);
}
else {
System.out.println(a);
}
}else {
if(b>c){
System.out.println(b);
}else {
System.out.println(c);
}
}
}
void Com(double a,double b){
System.out.println(a>b?a:b);
}
void Com(double a,double b,double c){
if(a>b){
if(c>a){
System.out.println(c);
}
else {
System.out.println(a);
}
}else {
if(b>c){
System.out.println(b);
}else {
System.out.println(c);
}
}
}
}
编程
定义一个中国邮政储蓄银行类,
属性:账号、密码、存款余额,银行名称。
方法:注册账号、存款、取款、查询。
创建该类对象并测试注册账号,存钱,取钱查询操作
要求:使用面向对象思想设计程序
package Day04.HM;
import java.util.Scanner;
/*
定义一个中国邮政储蓄银行类,
属性:账号、密码、存款余额,银行名称。
方法:注册账号、存款、取款、查询。
创建该类对象并测试注册账号,存钱,取钱查询操作
要求:使用面向对象思想设计程序
*/
public class Bank {
int account;
int password;
int yue;
String name = "中国邮政储蓄银行";
Scanner scanner = new Scanner(System.in);
public void register(){
System.out.println("请输入您的账号");
this.account = scanner.nextInt();
System.out.println("请输入您的密码");
this.password = scanner.nextInt();
System.out.println("注册成功");
}
public void deposit(){
System.out.println("请输入您要存入的金额:");
yue += scanner.nextInt();
System.out.println("存款成功,您当前余额为:"+yue);
}
public void withdrawal(){
System.out.println("请输入您要取走的金额:");
int money = scanner.nextInt();
if(money<=yue){
yue -= money;
System.out.println("取款成功,您当前余额为:"+yue);
}
System.out.println("余额不足!");
}
public void search(){
System.out.println(yue);
}
}
package Day04.HM;
public class BankTest {
public static void main(String[] args) {
Bank zs = new Bank();
out: while (true) {
System.out.println("请选择您的操作:1,注册2,取款3,存款4,查询");
if(zs.account==0&&zs.password==0){
System.out.println("请先注册账号");
zs.register();
}
int choice = 0;
switch (choice){
case 1:
zs.register();
case 2:
zs.withdrawal();
case 3:
zs.deposit();
case 4:
zs.search();
default:
break out;
}
}
}
}
简答
1.面向对象编程的思想.
面向对象的编程语言以分类的方式进行思考和解决问题。
面向对象先对整体关系作出分类,然后根据不同的类深入细节的处理。
2.说明类与对象的关系.
类包含对象,类是一个模板,它是描述一类事物(具体的对象,具体的存在)的属性和行为的集合.
对象是以类为模板在内存中创建的一个具体实例
3.简述构造方法的作用,特征,方法重载.
构造方法:创建对象的方法,没有返回值,不需要void修饰,每个类都有构造方法。如果没有显式地为类定义构造方法,Java将会为该类提供一
个默认构造方法,但是只要在一个Java类中定义了一个构造方法后,默认的无参构造方法即失效。一般情况下,定义有了有参的构造方法,最好把默认的无参的构造方法显示的写出来
方法重载:方法的重载是指同一个类中具有相同的名字,但参数不同的多个方法。
参数不同(可以有三方面的不同)
数量不同,类型不同,顺序不同
调用时,会根据不同的参数表选择对应的方法。
构造方法与成员方法都可以重载
注意:方法重载跟方法的返回值类型没有任何关系
4.简述静态变量和非静态变量的区别?
1.内存分配
静态变量在应用程序初始化时,就存在于内存当中,直到它所在的类的程序运行结束时才消亡;而非静态变量需要被实例化后才会分配内存。
2.生存周期
静态变量生存周期为应用程序的存在周期;非静态变量的存在周期取决于实例化的类的存在周期。
3.调用方式
静态变量只能通过“类.静态变量名”调用,类的实例不能调用;非静态变量当该变量所在的类被实例化后,可通过实例化的类名直接访问。
4.共享方式
静态变量是全局变量,被所有类的实例对象共享,即一个实例的改变了静态变量的值,其他同类的实例读到的就变化后的值;非静态变量是局部变量,不共享的。
5.访问方式
静态成员不能访问非静态成员;非静态成员可以访问静态成员。