- 写出你对面向对象语言的理解(自己举例)
就是一种把所有事物分成一类一类的,并挑出某一类中某一个体,以个体为单位的程序设计语言,即万物皆对象。
手机是一个手机大类,每一部手机都是一个对象,手机有自己的属性比如型号,内存,像素,价格等,也有行为(成员方法)比如可以拍照,电话,发短信等。编程时需要以手机个体
为单位进行设计,调用其属性或方法。
2.定义一个Student类
属性有学号,姓名,年龄,性别,语文成绩,数学成绩,英语成绩,
方法有:学习,考试(打印输出学习,考试即可))
创建两个学生对象,并给属性赋值和调用方法
public class Student {
private int stuID;
private String stuName;
private int stuAge;
private String stuSex;
private int chineseScores;
private int mathScores;
private int englishScores;
public Student(int stuID, String stuName, int stuAge, String stuSex, int chineseScores, int mathScores, int englishScores) {
this.stuID = stuID;
this.stuName = stuName;
this.stuAge = stuAge;
this.stuSex = stuSex;
this.chineseScores = chineseScores;
this.mathScores = mathScores;
this.englishScores = englishScores;
}
public Student() {
}
@Override
public String toString() {
return "Student{" +
"stuID=" + stuID +
", stuName='" + stuName + '\'' +
", stuAge=" + stuAge +
", stuSex='" + stuSex + '\'' +
", chineseScores=" + chineseScores +
", mathScores=" + mathScores +
", englishScores=" + englishScores +
'}';
}
public int getStuID() {
return stuID;
}
public void setStuID(int stuID) {
this.stuID = stuID;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
public String getStuSex() {
return stuSex;
}
public void setStuSex(String stuSex) {
this.stuSex = stuSex;
}
public int getChineseScores() {
return chineseScores;
}
public void setChineseScores(int chineseScores) {
this.chineseScores = chineseScores;
}
public int getMathScores() {
return mathScores;
}
public void setMathScores(int mathScores) {
this.mathScores = mathScores;
}
public int getEnglishScores() {
return englishScores;
}
public void setEnglishScores(int englishScores) {
this.englishScores = englishScores;
}
public void study(){
System.out.println(stuName+"学习");
}
public void test(){
System.out.println(stuName+"考试");
}
}
public class Dome {
public static void main(String[] args) {
Student stu1 = new Student();
stu1.setStuID(100001);
stu1.setStuName("张三");
stu1.setStuAge(20);
stu1.setStuSex("男");
stu1.setChineseScores(85);
stu1.setMathScores(90);
stu1.setEnglishScores(70);
stu1.study();
stu1.test();
System.out.println(stu1.toString());
Student stu2 = new Student(100002,"王五",20,"男",70,80,90);
stu2.study();
stu2.test();
System.out.println(stu2.toString());
}
}
3.定义长方形类,含:
属性:宽、高(整型);
方法:求周长、面积; (返回值)
在main方法中进行测试。
public class Rectangle {
private int length;
private int width;
public Rectangle() {
}
public Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int area(int length,int width){
return length*width;
}
public int perimeter(int length,int width){
return (length+width)*2;
}
@Override
public String toString() {
return "Rectangle{" +
"length=" + length +
", width=" + width +
'}';
}
}
public class Dome1 {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle();
System.out.println("长方形的面积为"+rectangle.area(20, 10));
System.out.println("长方形的周长为"+rectangle.perimeter(20,10));
}
}
4.定义一个中国银行类,
属性:账号、密码、余额,银行名称。
方法:注册账号,存款、取款、查询余额验证账号密码的方法
在main方法中创建该类对象并测试
public class BankofChina {
private int Id;
private int pressword;
private int balance;
private String bankName;
public int getId() {
return Id;
}
public void setId() {
Id = new Random().nextInt(100)+ 1000000;
}
public int getPressword() {
return pressword;
}
public void setPressword(int pressword) {
this.pressword = pressword;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
public String getBankName() {
return bankName;
}
public void setBankName(String bankName) {
this.bankName = bankName;
}
public BankofChina(int pressword, String bankName) {
//注册时咱不存款
Id = new Random().nextInt(100)+ 1000000;
this.pressword = pressword;
this.bankName = bankName;
}
public void deposit(int money){
balance +=money;
System.out.println("存款成功");
}
public void draw(int money){
balance -=money;
System.out.println("已出钞请尽快取走");
System.out.println("取款成功");
}
public void checkbalance(){
System.out.println("余额:"+balance);
}
public void checkpressword(){
while (true){
Scanner sc = new Scanner(System.in);
int psd =sc.nextInt();
if (this.pressword == psd) {
System.out.println("密码正确");
break;
}
else{
System.out.println("密码错误 请输入正确密码");
}
}
}
public BankofChina() {
}
@Override
public String toString() {
return "BankofChina{" +
"Id=" + Id +
", pressword=" + pressword +
", balance=" + balance +
", bankName='" + bankName + '\'' +
'}';
}
}
public class Dome3 {
public static void main(String[] args) {
System.out.println("请注册账号:设定六位数密码并选择银行");
Scanner sc = new Scanner(System.in);
int psd =sc.nextInt();
String name = sc.next();
BankofChina bankofChina = new BankofChina(psd ,name);
System.out.println("操作前请输入密码");
bankofChina.checkpressword();
System.out.println("我要存钱");
bankofChina.deposit(10000);
System.out.println("我要取钱");
bankofChina.draw(100);
System.out.println("我要查询余额");
bankofChina.checkbalance();
System.out.println(bankofChina.toString());
}
}
5.看程序写结果
5.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) {
Dog w = new Dog()
w.show();
}
}
名字:旺财 年龄:20
5.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
5.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
5.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]++;
}
}
5.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);
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
homework.Demo@1b6d3586
homework.Demo@1b6d3586
d.i : 5
d.i = 6
5.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
5.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; }
B
5.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