类的实例化
1 代码实现
编写一个Student类,包含name、gender、age、id、score属性,分别为String、String、int、int、double类型。
类中声明一个say方法,返回String类型,方法返回信息中包含所有属性值。
在另一个StudentTest类中的main方法中,创建Student对象,并访问say方法和所有属性,并将调用结果打印输出
package com.atguigu.exer1;
public class Student {
String name;
String gender;
int age;
int id;
double score;
public String say() {
return "姓名是" + name + ",性别是" + gender + ",年龄是"
+ age + ",学号是" + id + ",成绩是" + score;
}
}
package com.atguigu.exer1;
public class StudentTest {
public static void main(String[] args) {
Student stu = new Student();
stu.name = "九十九";
stu.gender = "男";
stu.age = 22;
stu.id = 48;
stu.score = 99.8;
String info = stu.say();
System.out.println(info);
}
}
编程题1
定义一个丈夫Husband类,有姓名、年龄、妻子属性
定义一个妻子Wife类,有姓名、年龄、丈夫属性
丈夫类中有一个getInfo方法,其中,能显示自己的姓名,年龄,和他的妻子的姓名,年龄
妻子类中有一个getInfo方法,其中,能显示自己的姓名,年龄,和她的丈夫的姓名,年龄
定义一个测试类,创建妻子和丈夫对象,然后测试
package com.atguigu.exer1;
public class Husband {
String hName;
int hAge;
//妻子属性
public Husband(String n,int a) {
this.hName = n;
this.hAge = a;
}
public void getInfo(Wife wife) {
String info = "丈夫名字:" + hName + ",丈夫年龄:" + hAge +
",妻子名字:" + wife.wName + ",妻子年龄" + wife.wAge;
System.out.println(info);
}
}
package com.atguigu.exer1;
public class Wife {
String wName;
int wAge;
public Wife(String n,int a) {
this.wName = n;
this.wAge = a;
}
public void getInfo(Husband husband) {
husband.getInfo(this);
String info = "妻子名字:" + wName + ",妻子年龄:" + wAge + ",丈夫名字:"
+ husband.hName + ",丈夫年龄:" + husband.hAge;
System.out.println(info);
}
}
package com.atguigu.exer1;
public class HWTest {
public static void main(String[] args) {
Husband h = new Husband("九十九",22);
Wife w = new Wife("九十八",20);
w.getInfo(h);
}
}
编程题2
定义银行账户类Account,有属性:卡号cid,余额balance,所属用户Customer
银行账户类Account有方法:
(1)getInfo(),返回String类型,返回卡的详细信息
(2)取钱方法withdraw(),参数自行设计,如果取钱成功返回true,失败返回false
(3)存钱方法save(),参数自行设计,如果存钱成功返回true,失败返回false
其中Customer类有姓名、身份证号、联系电话、家庭地址等属性
Customer类有方法say(),返回String类型,返回他的个人信息。
在测试类Bank中创建银行账户类对象和用户类对象,并设置信息,与显示信息
package com.atguigu.exer1;
public class Account {
int cid;
double balance;
//构造器 初始化属性
public Account(int c,double b) {
this.cid = c;
this.balance = b;
}
/**
* @Description : 返回账户的详细信息
* @ahthor : 099
* @data 2021年1月16日上午9:54:54
* @return 卡的详细信息
*/
public String getInfo() {
return ",卡号" + this.cid + ",余额" + this.balance;
}
/**
* @Description : 取钱方法 同时减少余额
* @ahthor : 099
* @data 2021年1月16日上午9:57:15
* @param amt
* @return true : 取钱成功 false:取钱失败
*/
public boolean withdraw(double amt) {
if(balance >= amt) {
balance -= amt;
return true;
}else {
return false;
}
}
/**
* @Description :存钱方法
* @ahthor : 099
* @data 2021年1月16日上午9:59:38
* @param amt
* @return true : 存钱成功 false:存钱失败
*/
public boolean save(double amt) {
if(amt > 0) {
balance += amt;
return true;
}else {
return false;
}
}
}
package com.atguigu.exer1;
public class Customer {
String name;
int IDnumber;
int phoneNumber;
String address;
private Account acc;
//构造器
public Customer(String n,int ID,int p,String a,Account acc) {
this.name = n;
this.IDnumber = ID;
this.phoneNumber = p;
this.address = a;
this.acc = acc;
}
// 定义Customer类 调用Account类的接口
public void setAccount(Account acc) {
this.acc = acc;
}
public Account getAccount() {
return this.acc;
}
public String say() {
return "用户姓名" + name + ",用户身份证" + IDnumber
+ ",用户地址" + address + ",用户电话" + phoneNumber +
acc.getInfo();
}
}
package com.atguigu.exer1;
public class Bank {
public static void main(String[] args) {
Account acc = new Account(99,2000);
Customer cus = new Customer("九十九",610502,158,"西安",acc);
if(cus.getAccount().withdraw(500)) {
System.out.println(cus.getAccount().getInfo());
}
if(cus.getAccount().save(1200)) {
System.out.println(cus.say());
}
}
}
方法的使用
1 哪个选项和show()方法重载
class Demo{
void show(int a,int b,float c){}
}
// 记住 两同一不同 类名同 方法名同 参数列表不同
A.void show(int a,float c,int b){}// yes
B,void show(int a,int b,float c){}// no
C.int show(int a,float c,int b){return a;}//yes
D.int show(int a,float c){return a;}//yes
方法的声明
(1)声明一个圆柱体类型,
(2)声明属性:底边的半径,和高
(3)声明方法:
A:方法的功能:在方法中打印圆柱体的详细信息
圆柱体的底边的半径是xxx,高是xxx,底面积是xxx,体积是xxx。
B:方法的功能:返回底面积
C:方法的功能:返回体积
D:方法的功能:为圆柱体的底边的半径,和高赋值
E:方法的功能:为圆柱体的底边的半径,和高赋值,并返回赋值的结果
如果底边的半径或高为<=0,赋值失败,返回false,否则返回true
package com.atguigu.exer1;
public class Cylinder {
double radius;
double height;
public void printInfo() {
System.out.println("圆柱体的底边的半径是" + radius + ",高是" + height +
",底面积是" + Math.PI*radius*radius + ",体积是" + Math.PI*radius*radius*height);
}
public double calArea() {
return Math.PI*radius*radius;
}
public double calVolume() {
return Math.PI*radius*radius*height;
}
public void setRadius(double r) {
this.radius = r;
}
public double getRadius() {
return this.radius;
}
public void setHeight(double h) {
this.height = h;
}
public double getHeight() {
return this.height;
}
public boolean setRH(double r,double h) {
this.radius = r;
this.height = h;
if(radius < 0 || height < 0) {
return false;
}else {
return true;
}
}
}
写出输出结果
package com.atguigu.exer1;
class Demo {
public static void main(String[] args) {
show(0);// 15
show(1);// 14
}
public static void show(int i) {
switch (i) {
default:
i += 2;
case 1:
i += 1;
case 4:
i += 8;
case 2:
i += 4;
}
System.out.println("i=" + i);
}
}
解释:
习惯上把default写在最下面,实际上default的顺序是无关的
当执行switch语句时,无论default位于那个位置,都会先检查每个case 常量的值是否与表达式相同 ,若相同,就会执行对应分支的case,全不相同的情况下,才会执行default
break关键在在switch结构中是可选的,表达式的值 与某个分支匹配后,直至遇到break或者执行完所有分支的语句才会跳出循环。
package com.atguigu.exer1;
class Demo {
public static void main(String[] args) {
int x = 1;
for (show('a'); show('b') && x < 3; show('c')) {
show('d');
x++;
}
}
public static boolean show(char ch) {
System.out.print(ch);
return true;
}
}
abdcbdcb
for循环结构的使用
一 循环结构的4个要素
1初始化条件
2循环条件 ---Boolean类型
3循环体
4迭代条件
二 for循环结构
for(初始化条件;循环条件;迭代条件){
循环体
}
执行过程:1-->2-->3-->4-->2--->4--->2.....-->2
面向对象性
1 面向对象三大特征的说明
答:面向对象有三大特点:封装、继承、多态。(如果要回答四个,可加上 抽象性 这一特点)
1.继承性:
继承是一种联结类的层次模型,并且允许和鼓励类的重用,它提供了一种明确表述共性的方法。对象的一个新类可以从现有的类中派生,这个过程称为类继承。新类继承了原始类的特性,新类称为原始类的派生类(子类),而原始类称为新类的基类(父类)。派生类可以从它的基类那里继承方法和实例变量,并且类可以修改或增加新的方法使之更适合特殊的需要。
2.封装性:
封装是把过程和数据包围起来,对数据的访问只能通过已定义的界面。面向对象计算始于这个基本概念,即现实世界可以被描绘成一系列完全自治、封装的对象,这些对象通过一个受保护的接口访问其他对象。
3. 多态性:
多态性是指允许不同类的对象对同一消息作出响应。多态性包括参数化多态性和包含多态性。多态性语言具有灵活、抽象、行为共享、代码共享的优势,很好的解决了应用程序函数同名问题。
4.抽象性:
抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面。抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用部分细节。抽象包括两个方面,一是过程抽象,二是数据抽象。
2 Java的内存管理之垃圾回收(了解)
分配:由JVM自动为其分配相应的内存空间
释放:由JVM提供垃圾回收机制自动的释放内存空间
垃圾回收机制(GC:Garbage Collection):将垃圾对象所占用的堆内存进行回收。Java的垃圾回收机制是JVM提供的能力,由单独的系统级垃圾回收线程在空闲时间以不定时的方式动态回收。
垃圾对象:不再被任何引用指向的对象。
构造器
1 构造器Constructor是否可被override
答:构造器Constructor不能被继承,因此不能重写Override,但可以被重载Overload(现在还没看懂 )
2.编程创建一个Box类,
在其中定义三个变量表示一个立方体的长、宽和高,定义一个方法求立方体的体积。创建一个对象,求给定尺寸的立方体的体积。
(提供无参的构造器和一个有参的构造器)
package com.atguigu.exer1;
public class BoxTest {
public static void main(String[] args) {
Box box = new Box(4.2,3,5);
double volume = box.calVolume();
System.out.println("输出该立方体的体积为:" + volume);
}
}
class Box{
double length;
double width;
double height;
public Box() {
}
public Box(double l,double w,double h) {
this.length = l;
this.width = w;
this.height = h;
}
public double calVolume() {
return length*width*height;
}
}
3 定义一个圆类型
提供显示圆周长功能的方法
提供显示圆面积的方法
提供无参的构造器和一个有参的构造器
package com.atguigu.exer1;
public class Circle {
double radius;
public Circle() {
}
public Circle(double c) {
this.radius = c;
}
public double showPeri() {
return 2*Math.PI*radius;
}
public double showArea() {
return Math.PI*radius*radius;
}
}
4 设计一个Dog类
有名字、颜色和年龄属性,定义构造器初始化这些属性,定义输出方法show()显示其信息。
package com.atguigu.exer1;
public class Dog {
String name;
String color;
int age;
public Dog() {
}
public Dog(String n,String c,int a) {
this.name = n;
this.color = c;
this.age = a;
}
public String showInfo() {
return "狗的名字:" + name + ",狗的颜色:" + color
+ ",狗的年龄:" + age;
}
}
5 写一个人的类
属性:名字,性别,年龄;提供无参的构造器和一个有参的构造器
方法:(1)自我介绍的方法(2)吃饭的方法
创建一个对象“张三”
package com.atguigu.exer1;
public class PersonTest {
public static void main(String[] args) {
Person p1 = new Person();
p1.name = "张三";
}
}
class Person{
String name;
String gender;
int age;
public Person() {
}
public Person(String n,String g,int a) {
this.name = n;
this.gender = g;
this.age = a;
}
public void showInfo() {
System.out.println("姓名:" + name + ",性别:" + gender
+ ",年龄:" + age);
}
public void eat() {
System.out.println(name + "在吃饭");
}
}
6 写一个课程类
属性:课程名;学时;任课老师;
创建五个对象:“c语言”,“java编程”,“php网络编程”,“c++”,“数据结构”
提供无参的构造器和一个有参的构造器
package com.atguigu.exer1;
public class LessonTest {
public static void main(String[] args) {
String[] name = new String[]{"c语言","java编程","php网络编程","c++","数据结构"};
Lesson[] les = new Lesson[5];
for(int i = 0;i < les.length;i++) {
les[i] = new Lesson(name[i]);
}
}
}
class Lesson{
String name;
String teacher;
public Lesson() {
}
public Lesson(String n) {
this.name = n;
}
}
7 以下程序的运行结果是
public class Test1 {
public static void main(String[] args) {
new A(new B());
}
}
class A{
public A(){
System.out.println("A");
}
public A(B b){
this();
System.out.println("AB");
}
}
class B{
public B(){
System.out.println("B");
}
}
B
A
AB
关于参数传递
1 写出结果
package com.atguigu.exer1;
public class Test {
public static void leftshift(int i, int j) {
i += j;
}
public static void main(String args[]) {
int i = 4, j = 2;
leftshift(i, j);
System.out.println(i);// 4
}
}
2 写出结果
package com.atguigu.exer1;
public class Test {
public static void main(String[] args) {
int[] a = new int[1];
modify(a);
System.out.println(a[0]); // 1
}
public static void modify(int[] a) {
a[0]++;
}
}
3 写出结果
this关键字
package com.atguigu.exer1;
public class Test {
public static void main(String[] args) {
TestA ta = new TestA();
System.out.println(ta.i); // 0
ta.change(ta.i);//1
System.out.println(ta.i); //0
ta.change1(ta); //1
System.out.println(ta.i);//1
}
}
class TestA {
int i;
void change(int i) {
i++;
System.out.println(i);
}
void change1(TestA t) {
t.i++;
System.out.println(t.i);
}
}
4 写出结果
this关键字
public class Test {
int x = 12;
public void method(int x) {
x += x;
System.out.println(x); // 10
}
}
Given:
Test t = new Test();
t.method(5);
5 写出结果
package com.atguigu.exer1;
public class Test {
public static void main(String[] args) {
int i = 0;
change(i);
// 此时i = 0
i = i++;
// 先将i(0)的值作为i++表达式的值
// 再将i加1,(i=1)
// 再将i++(0)的值赋给i
System.out.println("i = " + i);// 0
}
public static void change(int i) {
i++;
}
}
6 写出结果
String类型比较特殊,因为String类型是不可变的
如果形参经过操作修改的话,常量池中会开辟一个新的内存空间来存储修改后的数据
形参的改变对实参也没有影响。结果和值传递一样
package com.atguigu.exer1;
public class Test {
public static void main(String[] args) {
String str = new String("world");
char[] ch = new char[] { 'h', 'e', 'l', 'l', 'o' };
change(str, ch);
System.out.println(str);//world
System.out.println(String.valueOf(ch));//abcde
}
public static void change(String str, char[] arr) {
str = "change";
arr[0] = 'a';
arr[1] = 'b';
arr[2] = 'c';
arr[3] = 'd';
arr[4] = 'e';
}
}
7 写出下列结果
一定要画对象的内存解析结构图 给我恶心坏了
package com.atguigu.exer1;
public class Test {
int a;
int b;
public void f() {
a = 0;
b = 0;
int[] c = { 0 };
g(b, c);
System.out.println(a + " " + b + " " + c[0]);// 1 0 1
}
public void g(int b, int[] c) {// 这个b是形参 你就不能写个this关键字吗?
a = 1;
b = 1;
c[0] = 1;
}
public static void main(String[] args) {
Test t = new Test();
t.f();
}
}
//这一次做题给我人做傻了