package com.atguigu.exer;
public class Account {
private int id;//账号
private double balance;//余额
private double annualInterestRate;//年利率
public Account(int id, double balance, double annualInterestRate) {
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
//取钱
public void withdraw (double amount){
if(balance >= amount){
balance -= amount;
System.out.println("成功取出:" + amount);
}else{
System.out.println("余额不足,取款失败!");
}
}
//存钱
public void deposit (double amount){
balance += amount;
System.out.println("成功存入:" + amount);
}
}
package com.atguigu.exer;
public class Boy {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void marry(Girl girl){
System.out.println("我要娶" + girl.getName());
}
public void marry(Boy boy){
System.out.println("我要娶" + boy.getName());
}
public void shout(){
if(this.age >= 22){
System.out.println("我到了结婚年龄了!");
}else{
System.out.println("还是先谈谈恋爱吧");
}
}
}
package com.atguigu.exer;
public class Customer {
private String firstName;
private String lastName;
private Account account;
public Customer(String f, String l) {
this.firstName = f;
this.lastName = l;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
package com.atguigu.exer;
public class Girl {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void marry(Boy boy){
System.out.println("我要嫁给" + boy.getName());
boy.marry(this);
}
}
package com.atguigu.exer;
public class TestBoyGirl {
public static void main(String[] args) {
Boy boy = new Boy();
boy.setName(“工藤新一”);
boy.setAge(23);
Girl girl = new Girl();
girl.setName("小兰");
boy.marry(girl);
boy.shout();
System.out.println();
girl.marry(boy);
}
}
package com.atguigu.exer;
/*
-
(1) 创建一个Customer ,名字叫 Jane Smith,
-
他有一个账号为1000,余额为2000元,年利率为 1.23% 的账户。
(2) 对Jane Smith操作。
存入 100 元,再取出960元。再取出2000元。
打印出Jane Smith 的基本信息
Customer [Smith, Jane] has a account: id is 1000,
annualInterestRate is 1.23%, balance is 1140.0
*/
public class TestCustomer {
public static void main(String[] args) {
Customer cust = new Customer(“Jane”, “Smith”);
cust.setAccount(new Account(1000, 2000, 0.0123));
Account account = cust.getAccount();
account.deposit(100);
account.withdraw(960);
account.withdraw(2000);System.out.println("Customer [" + cust.getLastName() + "," + cust.getFirstName() + "] has a account: id is" + account.getId() + ",annualInterestRate is" + account.getAnnualInterestRate() * 100 + "%,balance is" + account.getBalance());}
}
package com.atguigu.exer1;
public class Circle {
private double radius;
public Circle(){
this.radius = 1;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
//计算圆的面积
public double findArea(){
return 3.14 * radius * radius;
}
}
package com.atguigu.exer1;
public class Cylinder extends Circle {
private double length;// 圆柱的高
public Cylinder() {
length = 1;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
// 重写父类Circle的方法,求圆柱的表面积
public double findArea() {
// return 3.14 * this.getRadius() * this.getRadius() * 2 + 2 * 3.14
// * this.getRadius() * this.length;
return super.findArea() * 2 + 2 * 3.14 * this.getRadius() * this.length;
}
// 圆柱的体积
public double findVolume() {
//return Math.PI * this.getRadius() * this.getRadius() * length;
return super.findArea() * length;
}
}
package com.atguigu.exer1;
/*
- 定义类Kids继承ManKind,并包括
成员变量int yearsOld;
方法printAge()打印yearsOld的值。
*/
public class Kids extends ManKind{
private int yearsOld;
public int getYearsOld() {
return yearsOld;
}
public void setYearsOld(int yearsOld) {
this.yearsOld = yearsOld;
}
public void printAge(){
System.out.println(this.yearsOld + " years old");
}
}
package com.atguigu.exer1;
/*
- 定义一个ManKind类,包括
成员变量int sex和int salary;
方法void manOrWorman():根据sex的值显示“man”(sex1)或者“woman”(sex0);
方法void employeed():根据salary的值显示“no job”(salary==0)或者“ job”(salary!=0)。
*/
public class ManKind {
private int sex;
private int salary;
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public void manOrWoman(){
if(sex == 1)
System.out.println("Man");
else if(sex == 0)
System.out.println("Woman");
else
System.out.println("输入的有误");
}
public void employees(){
if(salary == 0)
System.out.println("no job!");
else if(salary > 0)
System.out.println("job");
}
}
package com.atguigu.exer1;
public class TestCylinder {
public static void main(String[] args) {
Cylinder c = new Cylinder();
double area = c.findVolume();
System.out.println(area);
c.setRadius(2.3);
c.setLength(1.2);
area = c.findVolume();
System.out.println(area);
c.findArea();//求圆柱的表面积
}
}
package com.atguigu.exer1;
/*
- 在Kids类的main方法中实例化Kids的对象someKid,用该对象访问其父类的成员变量及方法。
*/
public class TestKids {
public static void main(String[] args) {
Kids someKid = new Kids();
someKid.setSalary(0);
someKid.setSex(1);
someKid.setYearsOld(13);
someKid.employees();
someKid.manOrWoman();
someKid.printAge();
}
}
package com.atguigu.java;
/*
-
package:声明源文件所在的包,写在程序的第一行。
-
每“.”一次,表示一层文件目录。
-
包名都要小写。
-
import:
-
1)显式导入指定包下的类或接口
-
2)写在包的声明和源文件之间
-
3)如果需要引入多个类或接口,那么就并列写出
-
4)如果导入的类是java.lang包下的,如:System String Math等,就不需要显式的声明。
-
5)理解.的概念。比如java.util.;
-
6)如何处理同名类的导入。如:在util包和sql包下同时存在Date类。
-
7)import static 表示导入指定类的static的属性或方法
-
8)导入java.lang.只能导入lang包下的所有类或接口,不能导入lang的子包下的类或接口
/
//import java.util.Scanner;
//import java.util.Date;
//import java.util.List;
//import java.util.ArrayList;
import java.lang.reflect.Field;
import java.util.;
import static java.lang.System.;
public class TestPackageImport {
public static void main(String[] args) {
out.println(“helloworld”);
Scanner s = new Scanner(System.in);
s.next();Date d = new Date(); List list = new ArrayList(); java.sql.Date d1 = new java.sql.Date(522535114234L); Field f = null;}
}
package com.atguigu.java;
/*
- this:
- 1.可以用来修饰属性、方法、构造器
- 2.this理解为当前对象或当前正在创建的对象.比如:this.name,this.show();
- 3.可以在构造器中通过“this(形参)”的方式显示的调用本类中其它重载的指定的构造器。
- 要求:1.在构造器内部必须声明在首行!
-
2.若一个类中有n个构造器,那么最多有n-1个构造器中使用了this(形参);
*/
public class TestPerson {
public static void main(String[] args) {
Person p = new Person();
p.setAge(10);
p.info();
p.setName(“李雷”);
p.info();
System.out.println();
Person p1 = new Person("韩梅梅",23);
p1.info();
Person p2 = new Person(23);
p2.info();
}
}
class Person{
private String name;
private int age;
public Person(){
// this(“AA”);
name = “Lucy”;
age = 1;
}
public Person(String name){
this(12);
System.out.println(“这是Person类中形参为name的构造器”);
this.name = name;
}
public Person(int age){
this();
this.age = age;
}
//this.name:表示当前正在创建的对象
//name:是形参
public Person(String name,int age){
// this.name = name;
//this():可以用来显示的调用当前类的重载的指定的构造器。
this(name);
System.out.println(“这是Person类中形参为name和age的构造器”);
this.age = age;
}
//this.name:表示当前对象的属性
//name:是形参
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
public void eat(){
System.out.println("人吃饭");
}
public void sleep(){
System.out.println("人睡觉");
}
//
public void info(){
System.out.println("name: " + this.name + " age: " + this.age);
this.show();
}
public void show(){
System.out.println("我是一个人,我的名字是:" + this.name);
}
}
package com.atguigu.java;
/*
- 编写两个类,TriAngle和TestTriAngle,其中TriAngle中声明私有的底边长base和高height,
- 同时声明公共方法访问私有变量;另一个类中使用这些公共方法,计算三角形的面积。
*/
public class TestTriAngle { //Angle:角 Angel:天使 host guest
public static void main(String[] args) {
TriAngle t = new TriAngle();
t.setBase(2.3);
t.setHeight(1.2);
System.out.println(“面积为:” + t.findArea());
}
}
class TriAngle{//三角形
private double base;//底边长
private double height;//高
public TriAngle(){
this.base = 1.0;
this.height = 1.0;
}
public TriAngle(double base,double height){
this.base = base;
this.height = height;
}
public double getBase(){
return base;
}
public double getHeight(){
return height;
}
public void setBase(double base){
this.base = base;
}
public void setHeight(double height){
this.height = height;
}
public double findArea(){
return this.base * this.height / 2;
}
}
package com.atguigu.java1;
public class Graduate extends Student{
public void show(){
System.out.println(“我是一个研究生”);
}
}
package com.atguigu.java1;
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void eat() {
System.out.println("吃饭");
}
void walk() {
System.out.println("走路");
}
private void sleep(){
}
}
package com.atguigu.java1;
class A{
}
public class Student extends Person{
//public class Student extends Person,A{
// private String name;
// private int age;
private String schoolName;
public Student(){
}
public Student(String name,int age){
// this.name = name;
// this.age = age;
this.setName(name);
this.setAge(age);
}
// public String getName() {
// return name;
// }
// public void setName(String name) {
// this.name = name;
// }
// public int getAge() {
// return age;
// }
// public void setAge(int age) {
// this.age = age;
// }
//
// 对父类同名的方法的重写、覆盖
public void eat(){
System.out.println(“应该多吃有营养的”);
}
public void walk(){
System.out.println(“背着书包蹦蹦跳跳的走路”);
}
public void info(){
eat();
//System.out.println(“name:” + name);
System.out.println(“我是一个学生”);
}
//不是对父类私有的sleep()方法的重写。
private int sleep(){
return 0;
}
}
package com.atguigu.java1;
/*
- 一、面向对象的特征二:继承性
- 1.为什么要设计继承性?
- 2.通过"class A extends B"类实现类的继承。
- 子类:A 父类(或基类 SuperClass):B
- 3.子类继承父类以后,父类中声明的属性、方法,子类就可以获取到。
- 明确:当父类中有私有的属性或方法时,子类同样可以获取得到,只是由于封装性的设计,使得子类不可以直接
-
调用罢了。 - 子类除了通过继承,获取父类的结构之外,还可以定义自己的特有的成分。
- extends:子类是对父类功能的“扩展”,明确子类不是父类的子集。
- 4.java中类的继承性只支持单继承:一个类只能继承一个父类。反之,一个父类可以有多个子类。
- 5.子父类是相对的概念。
- 二、方法的重写 —(方法的重载) 修饰符 返回值类型 方法名 (参数列表){}
- 1.前提:有子类继承父类
- 2.子类继承父类以后,若父类的方法对子类不适用,那么子类可以对父类的方法重写(override overwrite)、覆盖、覆写。
- 3.重写的规则: 1)要求子类方法的“返回值类型 方法名 (参数列表)”与父类的方法一样
-
2)子类方法的修饰符不能小于父类方法的修饰符 -
3)*若父类方法抛异常,那么子类方法抛的异常类型不能大于父类的。 -
4)*子父类的方法必须同为static或同为非static的。
*/
public class TestExtends {
public static void main(String[] args) {
Student s = new Student();
s.eat();
Worker w = new Worker();
w.eat();
Person p = new Person();
p.eat();
s.setAge(12);
s.setName("麻华龙");
System.out.println(s.getName() + ":" + s.getAge());
Graduate g = new Graduate();
g.eat();
g.info();
g.show();
}
}
package com.atguigu.java1;
public class Worker extends Person{
public void eat(){
System.out.println(“工人吃饭”);
}
public void walk(){
}
}
这篇博客介绍了JAVA基础中的类定义和对象操作。通过`Account`、`Boy`、`Customer`等类展示了如何定义属性和方法。还涵盖了继承的概念,如`Girl`继承自`Boy`,以及`Cylinder`类如何扩展`Circle`类。此外,讲解了如何在子类中调用父类的构造器和方法,并展示了`Customer`类的实例化以及`TestCustomer`类中对账户的操作,如存款、取款等。

被折叠的 条评论
为什么被折叠?



