尚硅谷java基础学习笔记
day12
com.atguigu.exer.ManKind.java,com.atguigu.exer.KidsTest.java,com.atguigu.exer.Kids.java
package com. atguigu. exer;
public class Kids extends ManKind {
private int yearsOld;
public Kids ( ) {
}
public Kids ( int yearsOld) {
this . yearsOld = yearsOld;
}
public void printAge ( ) {
System. out. println ( "I am " + yearsOld + " years old." ) ;
}
public int getYearsOld ( ) {
return yearsOld;
}
public void setYearsOld ( int yearsOld) {
this . yearsOld = yearsOld;
}
public void employeed ( ) {
System. out. println ( "Kids should study and no job." ) ;
}
}
package com. atguigu. exer;
public class KidsTest {
public static void main ( String[ ] args) {
Kids someKid = new Kids ( 12 ) ;
someKid. printAge ( ) ;
someKid. setSalary ( 0 ) ;
someKid. setSex ( 1 ) ;
someKid. employeed ( ) ;
someKid. manOrWoman ( ) ;
}
}
package com. atguigu. exer;
public class ManKind {
private int sex;
private int salary;
public ManKind ( ) {
}
public ManKind ( int sex, int salary) {
this . sex = sex;
this . salary = salary;
}
public void manOrWoman ( ) {
if ( sex == 1 ) {
System. out. println ( "man" ) ;
} else if ( sex == 0 ) {
System. out. println ( "woman" ) ;
}
}
public void employeed ( ) {
String jobInfo = ( salary == 0 ) ? "no job" : "job" ;
System. out. println ( jobInfo) ;
}
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;
}
}
com.atguigu.exer1
package com. atguigu. exer1;
public class Circle {
private double radius;
public Circle ( ) {
radius = 1.0 ;
}
public Circle ( double radius) {
this . radius = radius;
}
public double getRadius ( ) {
return radius;
}
public void setRadius ( double radius) {
this . radius = radius;
}
public double findArea ( ) {
return Math. PI * radius * radius;
}
}
package com. atguigu. exer1;
public class Cylinder extends Circle {
private double length;
public Cylinder ( ) {
length = 1.0 ;
}
public double getLength ( ) {
return length;
}
public void setLength ( double length) {
this . length = length;
}
public double findVolume ( ) {
return super . findArea ( ) * getLength ( ) ;
}
@Override
public double findArea ( ) {
return Math. PI * getRadius ( ) * getRadius ( ) * 2 +
2 * Math. PI * getRadius ( ) * getLength ( ) ;
}
}
package com. atguigu. exer1;
public class CylinderTest {
public static void main ( String[ ] args) {
Cylinder cy = new Cylinder ( ) ;
cy. setRadius ( 2.1 ) ;
cy. setLength ( 3.4 ) ;
double volume = cy. findVolume ( ) ;
System. out. println ( "圆柱的体积为:" + volume) ;
double area = cy. findArea ( ) ;
System. out. println ( "圆柱的表面积:" + area) ;
System. out. println ( "******************" ) ;
Cylinder cy1 = new Cylinder ( ) ;
double volume1 = cy1. findVolume ( ) ;
System. out. println ( "圆柱的体积为:" + volume1) ;
}
}
com.atguigu.java.DebugTest.java,DebugTest1.java
package com. atguigu. java;
public class DebugTest {
public static void main ( String[ ] args) {
int i = 10 ;
int j = 20 ;
System. out. println ( "i = " + i + ", j = " + j) ;
DebugTest test = new DebugTest ( ) ;
int max = test. getMax ( i, j) ;
System. out. println ( "max = " + max) ;
}
private int getMax ( int k, int m) {
int max = 0 ;
if ( k < m) {
max = k;
} else {
max = m;
}
return max;
}
}
package com. atguigu. java;
public class DebugTest1 {
public static void main ( String[ ] args) {
int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 , 5 } ;
System. out. println ( arr) ;
char [ ] arr1 = new char [ ] { 'a' , 'b' , 'c' } ;
System. out. println ( arr1) ;
}
}
com.atguigu.java1 Person.java Student.java PersonTest.java
package com. atguigu. java1;
public class PersonTest {
public static void main ( String[ ] args) {
Student s = new Student ( "计算机科学与技术" ) ;
s. eat ( ) ;
s. walk ( 10 ) ;
System. out. println ( "**************" ) ;
s. study ( ) ;
Person p1 = new Person ( ) ;
p1. eat ( ) ;
}
}
package com. atguigu. java1;
public class Person {
String name;
int age;
public Person ( ) {
}
public Person ( String name, int age) {
this . name = name;
this . age = age;
}
void eat ( ) {
System. out. println ( "吃饭" ) ;
}
public void walk ( int distance) {
System. out. println ( "走路,走的距离是:" + distance + "公里" ) ;
show ( ) ;
eat ( ) ;
}
private void show ( ) {
System. out. println ( "我是一个人" ) ;
}
public Object info ( ) {
return null;
}
public double info1 ( ) {
return 1.0 ;
}
}
package com. atguigu. java1;
public class Student extends Person {
String major;
public Student ( ) {
}
public Student ( String major) {
this . major = major;
}
public void study ( ) {
System. out. println ( "学习。专业是:" + major) ;
}
public void eat ( ) {
System. out. println ( "学生应该多吃有营养的食物" ) ;
}
public void show ( ) {
System. out. println ( "我是一个学生" ) ;
}
public String info ( ) {
return null;
}
public void walk ( int distance) {
System. out. println ( "重写的方法" ) ;
}
}
com.atguigu.java2 Order.java OrderTest.java
package com. atguigu. java2;
public class Order {
private int orderPrivate;
int orderDefault;
protected int orderProtected;
public int orderPublic;
private void methodPrivate ( ) {
orderPrivate = 1 ;
orderDefault = 2 ;
orderProtected = 3 ;
orderPublic = 4 ;
}
void methodDefault ( ) {
orderPrivate = 1 ;
orderDefault = 2 ;
orderProtected = 3 ;
orderPublic = 4 ;
}
protected void methodProtected ( ) {
orderPrivate = 1 ;
orderDefault = 2 ;
orderProtected = 3 ;
orderPublic = 4 ;
}
public void methodPublic ( ) {
orderPrivate = 1 ;
orderDefault = 2 ;
orderProtected = 3 ;
orderPublic = 4 ;
}
}
package com. atguigu. java2;
public class OrderTest {
public static void main ( String[ ] args) {
Order order = new Order ( ) ;
order. orderDefault = 1 ;
order. orderProtected = 2 ;
order. orderPublic = 3 ;
order. methodDefault ( ) ;
order. methodProtected ( ) ;
order. methodPublic ( ) ;
}
}
com.atguigu.java3 Person.java Student.java OrderTest .java SubOrder.java
package com. atguigu. java3;
public class Person {
String name;
int age;
int id = 1001 ;
public Person ( ) {
System. out. println ( "我无处不在!" ) ;
}
public Person ( String name) {
this . name = name;
}
public Person ( String name, int age) {
this ( name) ;
this . age = age;
}
public void eat ( ) {
System. out. println ( "人:吃饭" ) ;
}
public void walk ( ) {
System. out. println ( "人:走路" ) ;
}
}
package com. atguigu. java3;
public class Student extends Person {
String major;
int id = 1002 ;
public Student ( ) {
super ( ) ;
}
public Student ( String major) {
super ( ) ;
this . major = major;
}
public Student ( String name, int age, String major) {
super ( name, age) ;
this . major = major;
}
@Override
public void eat ( ) {
System. out. println ( "学生:多吃有营养的食物" ) ;
}
public void study ( ) {
System. out. println ( "学生:学习知识" ) ;
this . eat ( ) ;
super . eat ( ) ;
walk ( ) ;
}
public void show ( ) {
System. out. println ( "name = " + name + ", age = " + age) ;
System. out. println ( "id = " + this . id) ;
System. out. println ( "id = " + super . id) ;
}
}
package com. atguigu. java3;
import com. atguigu. java2. Order;
public class OrderTest {
public static void main ( String[ ] args) {
Order order = new Order ( ) ;
order. orderPublic = 1 ;
order. methodPublic ( ) ;
}
public void show ( Order order) {
order. orderPublic = 1 ;
order. methodPublic ( ) ;
}
}
package com. atguigu. java3;
import com. atguigu. java2. Order;
public class SubOrder extends Order {
public void method ( ) {
orderProtected = 1 ;
orderPublic = 2 ;
methodProtected ( ) ;
methodPublic ( ) ;
}
}
com.atguigu.java3 SuperTest.java InstanceTest.java
package com. atguigu. java3;
public class SuperTest {
public static void main ( String[ ] args) {
Student s = new Student ( ) ;
s. show ( ) ;
System. out. println ( ) ;
s. study ( ) ;
Student s1 = new Student ( "Tom" , 21 , "IT" ) ;
s1. show ( ) ;
System. out. println ( "************" ) ;
Student s2 = new Student ( ) ;
}
}
package com. atguigu. java3;
public class InstanceTest {
}
com.atguigu.exer2
package com. atguigu. exer2;
public class Account {
private int id;
private double balance;
private double annualInterestRate;
public Account ( int id, double balance, double annualInterestRate) {
super ( ) ;
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 double getMonthlyInterest ( ) {
return annualInterestRate / 12 ;
}
public void withdraw ( double amount) {
if ( balance >= amount) {
balance -= amount;
return ;
}
System. out. println ( "余额不足" ) ;
}
public void deposit ( double amount) {
if ( amount > 0 ) {
balance += amount;
}
}
}
package com. atguigu. exer2;
public class AccountTest {
public static void main ( String[ ] args) {
Account acct = new Account ( 1122 , 20000 , 0.045 ) ;
acct. withdraw ( 30000 ) ;
System. out. println ( "您的账户余额为:" + acct. getBalance ( ) ) ;
acct. withdraw ( 2500 ) ;
System. out. println ( "您的账户余额为:" + acct. getBalance ( ) ) ;
acct. deposit ( 3000 ) ;
System. out. println ( "您的账户余额为:" + acct. getBalance ( ) ) ;
System. out. println ( "月利率为:" + ( acct. getMonthlyInterest ( ) * 100 ) + "%" ) ;
}
}
package com. atguigu. exer2;
public class CheckAccount extends Account {
private double overdraft;
public CheckAccount ( int id, double balance, double annualInterestRate, double overdraft) {
super ( id, balance, annualInterestRate) ;
this . overdraft = overdraft;
}
public double getOverdraft ( ) {
return overdraft;
}
public void setOverdraft ( double overdraft) {
this . overdraft = overdraft;
}
@Override
public void withdraw ( double amount) {
if ( getBalance ( ) >= amount) {
super . withdraw ( amount) ;
} else if ( overdraft >= amount - getBalance ( ) ) {
overdraft -= ( amount - getBalance ( ) ) ;
super . withdraw ( getBalance ( ) ) ;
} else {
System. out. println ( "超过可透支限额!" ) ;
}
}
}
package com. atguigu. exer2;
public class CheckAccountTest {
public static void main ( String[ ] args) {
CheckAccount acct = new CheckAccount ( 1122 , 20000 , 0.045 , 5000 ) ;
acct. withdraw ( 5000 ) ;
System. out. println ( "您的账户余额为:" + acct. getBalance ( ) ) ;
System. out. println ( "您的可透支额度为:" + acct. getOverdraft ( ) ) ;
acct. withdraw ( 18000 ) ;
System. out. println ( "您的账户余额为:" + acct. getBalance ( ) ) ;
System. out. println ( "您的可透支额度为:" + acct. getOverdraft ( ) ) ;
acct. withdraw ( 3000 ) ;
System. out. println ( "您的账户余额为:" + acct. getBalance ( ) ) ;
System. out. println ( "您的可透支额度为:" + acct. getOverdraft ( ) ) ;
}
}
com.atguigu.java4
package com. atguigu. java4;
import java. sql. Connection;
public class AnimalTest {
public static void main ( String[ ] args) {
AnimalTest test = new AnimalTest ( ) ;
test. func ( new Dog ( ) ) ;
test. func ( new Cat ( ) ) ;
}
public void func ( Animal animal) {
animal. eat ( ) ;
animal. shout ( ) ;
if ( animal instanceof Dog ) {
Dog d = ( Dog) animal;
d. watchDoor ( ) ;
}
}
}
class Animal {
public void eat ( ) {
System. out. println ( "动物:进食" ) ;
}
public void shout ( ) {
System. out. println ( "动物:叫" ) ;
}
}
class Dog extends Animal {
public void eat ( ) {
System. out. println ( "狗吃骨头" ) ;
}
public void shout ( ) {
System. out. println ( "汪!汪!汪!" ) ;
}
public void watchDoor ( ) {
System. out. println ( "看门" ) ;
}
}
class Cat extends Animal {
public void eat ( ) {
System. out. println ( "猫吃鱼" ) ;
}
public void shout ( ) {
System. out. println ( "喵!喵!喵!" ) ;
}
}
class Order {
public void method ( Object obj) {
}
}
class Driver {
public void doData ( Connection conn) {
}
}
package com. atguigu. java4;
public class Man extends Person {
boolean isSmoking;
int id = 1002 ;
public void earnMoney ( ) {
System. out. println ( "男人负责挣钱养家" ) ;
}
public void eat ( ) {
System. out. println ( "男人多吃肉,长肌肉" ) ;
}
public void walk ( ) {
System. out. println ( "男人霸气的走路" ) ;
}
}
package com. atguigu. java4;
public class Person {
String name;
int age;
int id = 1001 ;
public void eat ( ) {
System. out. println ( "人:吃饭" ) ;
}
public void walk ( ) {
System. out. println ( "人:走路" ) ;
}
}
package com. atguigu. java4;
public class PersonTest {
public static void main ( String[ ] args) {
Person p1 = new Person ( ) ;
p1. eat ( ) ;
Man man = new Man ( ) ;
man. eat ( ) ;
man. age = 25 ;
man. earnMoney ( ) ;
System. out. println ( "*******************" ) ;
Person p2 = new Man ( ) ;
p2. eat ( ) ;
p2. walk ( ) ;
System. out. println ( p2. id) ;
}
}
package com. atguigu. java4;
public class Woman extends Person {
boolean isBeauty;
public void goShopping ( ) {
System. out. println ( "女人喜欢购物" ) ;
}
public void eat ( ) {
System. out. println ( "女人少吃,为了减肥" ) ;
}
public void walk ( ) {
System. out. println ( "女人窈窕的走路" ) ;
}
}
package com. atguigu. java5;
import java. util. Random;
class Animal {
protected void eat ( ) {
System. out. println ( "animal eat food" ) ;
}
}
class Cat extends Animal {
protected void eat ( ) {
System. out. println ( "cat eat fish" ) ;
}
}
class Dog extends Animal {
public void eat ( ) {
System. out. println ( "Dog eat bone" ) ;
}
}
class Sheep extends Animal {
public void eat ( ) {
System. out. println ( "Sheep eat grass" ) ;
}
}
public class InterviewTest {
public static Animal getInstance ( int key) {
switch ( key) {
case 0 :
return new Cat ( ) ;
case 1 :
return new Dog ( ) ;
default :
return new Sheep ( ) ;
}
}
public static void main ( String[ ] args) {
int key = new Random ( ) . nextInt ( 3 ) ;
System. out. println ( key) ;
Animal animal = getInstance ( key) ;
animal. eat ( ) ;
}
}