尚硅谷java基础学习笔记
day14
com.atguigu.java
package com. atguigu. java;
public class Person {
String name;
int age;
public Person ( String name, int age) {
super ( ) ;
this . name = name;
this . age = age;
}
public void eat ( ) {
System. out. println ( "人:吃饭" ) ;
}
public void walk ( ) {
System. out. println ( "人:走路" ) ;
}
@Override
public boolean equals ( Object obj) {
if ( this == obj)
return true ;
if ( obj == null)
return false ;
if ( getClass ( ) != obj. getClass ( ) )
return false ;
Person other = ( Person) obj;
if ( age != other. age)
return false ;
if ( name == null) {
if ( other. name != null)
return false ;
} else if ( ! name. equals ( other. name) )
return false ;
return true ;
}
}
package com. atguigu. java;
public class Man extends Person {
public Man ( String name, int age) {
super ( name, age) ;
}
public void earnMoney ( ) {
System. out. println ( "男人负责挣钱养家" ) ;
}
public void eat ( ) {
System. out. println ( "男人多吃肉,长肌肉" ) ;
}
public void walk ( ) {
System. out. println ( "男人霸气的走路" ) ;
}
}
package com. atguigu. java;
import org. junit. Test;
public class ReviewTest {
@Test
public void test3 ( ) {
String s = "abc" ;
s = null;
System. out. println ( s) ;
System. out. println ( "*********" ) ;
System. out. println ( s. toString ( ) ) ;
}
@Test
public void test2 ( ) {
Person p = new Person ( "Tom" , 12 ) ;
Man m = new Man ( "Tom" , 12 ) ;
System. out. println ( p. equals ( m) ) ;
}
@Test
public void test1 ( ) {
int [ ] arr = new int [ ] { 1 , 2 , 3 } ;
print ( arr) ;
System. out. println ( arr. getClass ( ) . getSuperclass ( ) ) ;
}
public void print ( Object obj) {
System. out. println ( obj) ;
}
}
com.atguigu.exer
package com. atguigu. exer;
public class Account {
private int id;
private String pwd = "000000" ;
private double balance;
private static double interestRate;
private static double minMoney = 1.0 ;
private static int init = 1001 ;
public Account ( ) {
id = init++ ;
}
public Account ( String pwd, double balance) {
id = init++ ;
this . pwd = pwd;
this . balance = balance;
}
public String getPwd ( ) {
return pwd;
}
public void setPwd ( String pwd) {
this . pwd = pwd;
}
public static double getInterestRate ( ) {
return interestRate;
}
public static void setInterestRate ( double interestRate) {
Account. interestRate = interestRate;
}
public static double getMinMoney ( ) {
return minMoney;
}
public static void setMinMoney ( double minMoney) {
Account. minMoney = minMoney;
}
public int getId ( ) {
return id;
}
public double getBalance ( ) {
return balance;
}
@Override
public String toString ( ) {
return "Account [id=" + id + ", pwd=" + pwd + ", balance=" + balance + "]" ;
}
}
package com. atguigu. exer;
public class AccountTest {
public static void main ( String[ ] args) {
Account acct1 = new Account ( ) ;
Account acct2 = new Account ( "qwerty" , 2000 ) ;
Account. setInterestRate ( 0.012 ) ;
Account. setMinMoney ( 100 ) ;
System. out. println ( acct1) ;
System. out. println ( acct2) ;
System. out. println ( acct1. getInterestRate ( ) ) ;
System. out. println ( acct1. getMinMoney ( ) ) ;
}
}
com.atguigu.java1
package com. atguigu. java1;
public class ArrayUtil {
public static int getMax ( int [ ] arr) {
int maxValue = arr[ 0 ] ;
for ( int i = 1 ; i < arr. length; i++ ) {
if ( maxValue < arr[ i] ) {
maxValue = arr[ i] ;
}
}
return maxValue;
}
public static int getMin ( int [ ] arr) {
int minValue = arr[ 0 ] ;
for ( int i = 1 ; i < arr. length; i++ ) {
if ( minValue > arr[ i] ) {
minValue = arr[ i] ;
}
}
return minValue;
}
public static int getSum ( int [ ] arr) {
int sum = 0 ;
for ( int i = 0 ; i < arr. length; i++ ) {
sum += arr[ i] ;
}
return sum;
}
public static int getAvg ( int [ ] arr) {
return getSum ( arr) / arr. length;
}
public static void reverse ( int [ ] arr) {
for ( int i = 0 ; i < arr. length / 2 ; i++ ) {
int temp = arr[ i] ;
arr[ i] = arr[ arr. length - i - 1 ] ;
arr[ arr. length - i - 1 ] = temp;
}
}
public static int [ ] copy ( int [ ] arr) {
int [ ] arr1 = new int [ arr. length] ;
for ( int i = 0 ; i < arr1. length; i++ ) {
arr1[ i] = arr[ i] ;
}
return arr1;
}
public static void sort ( int [ ] arr) {
for ( int i = 0 ; i < arr. length - 1 ; i++ ) {
for ( int j = 0 ; j < arr. length - 1 - i; j++ ) {
if ( arr[ j] > arr[ j + 1 ] ) {
swap ( arr, j, j + 1 ) ;
}
}
}
}
private static void swap ( int [ ] arr, int i, int j) {
int temp = arr[ i] ;
arr[ i] = arr[ j] ;
arr[ j] = temp;
}
public static void print ( int [ ] arr) {
for ( int i = 0 ; i < arr. length; i++ ) {
System. out. print ( arr[ i] + "\t" ) ;
}
System. out. println ( ) ;
}
public static int getIndex ( int [ ] arr, int dest) {
for ( int i = 0 ; i < arr. length; i++ ) {
if ( dest == arr[ i] ) {
return i;
}
}
return - 1 ;
}
}
package com. atguigu. java1;
public class ArrayUtilTest {
public static void main ( String[ ] args) {
int [ ] arr = new int [ ] { 32 , 34 , 32 , 5 , 3 , 54 , 654 , - 98 , 0 , - 53 , 5 } ;
int max = ArrayUtil. getMax ( arr) ;
System. out. println ( "最大值为:" + max) ;
System. out. println ( "排序前:" ) ;
ArrayUtil. print ( arr) ;
ArrayUtil. sort ( arr) ;
System. out. println ( "排序后:" ) ;
ArrayUtil. print ( arr) ;
}
}
package com. atguigu. java1;
public class StaticTest {
public static void main ( String[ ] args) {
Chinese. nation = "中国" ;
Chinese c1 = new Chinese ( ) ;
c1. name = "姚明" ;
c1. age = 40 ;
c1. nation = "CHN" ;
Chinese c2 = new Chinese ( ) ;
c2. name = "马龙" ;
c2. age = 30 ;
c2. nation = "CHINA" ;
System. out. println ( c1. nation) ;
c1. eat ( ) ;
Chinese. show ( ) ;
}
}
class Chinese {
String name;
int age;
static String nation;
public void eat ( ) {
System. out. println ( "中国人吃中餐" ) ;
this . info ( ) ;
System. out. println ( "name :" + name) ;
walk ( ) ;
System. out. println ( "nation : " + nation) ;
}
public static void show ( ) {
System. out. println ( "我是一个中国人!" ) ;
System. out. println ( Chinese. nation) ;
walk ( ) ;
}
public void info ( ) {
System. out. println ( "name :" + name + ",age : " + age) ;
}
public static void walk ( ) {
}
}
package com. atguigu. java1;
public class CircleTest {
public static void main ( String[ ] args) {
Circle c1 = new Circle ( ) ;
Circle c2 = new Circle ( ) ;
Circle c3 = new Circle ( 3.4 ) ;
System. out. println ( "c1的id:" + c1. getId ( ) ) ;
System. out. println ( "c2的id:" + c2. getId ( ) ) ;
System. out. println ( "c3的id:" + c3. getId ( ) ) ;
System. out. println ( "创建的圆的个数为:" + Circle. getTotal ( ) ) ;
}
}
class Circle {
private double radius;
private int id;
public Circle ( ) {
id = init++ ;
total++ ;
}
public Circle ( double radius) {
this ( ) ;
this . radius = radius;
}
private static int total;
private static int init = 1001 ;
public double findArea ( ) {
return 3.14 * radius * radius;
}
public double getRadius ( ) {
return radius;
}
public void setRadius ( double radius) {
this . radius = radius;
}
public int getId ( ) {
return id;
}
public static int getTotal ( ) {
return total;
}
}
com.atguigu.java2
package com. atguigu. java2;
public class SingletonTest1 {
public static void main ( String[ ] args) {
Bank bank1 = Bank. getInstance ( ) ;
Bank bank2 = Bank. getInstance ( ) ;
System. out. println ( bank1 == bank2) ;
}
}
class Bank {
private Bank ( ) {
}
private static Bank instance = new Bank ( ) ;
public static Bank getInstance ( ) {
return instance;
}
}
package com. atguigu. java2;
public class SingletonTest2 {
public static void main ( String[ ] args) {
Order order1 = Order. getInstance ( ) ;
Order order2 = Order. getInstance ( ) ;
System. out. println ( order1 == order2) ;
}
}
class Order {
private Order ( ) {
}
private static Order instance = null;
public static Order getInstance ( ) {
if ( instance == null) {
instance = new Order ( ) ;
}
return instance;
}
}
package com. atguigu. java2;
public class MainTest {
public static void main ( String[ ] args) {
Main. main ( new String [ 100 ] ) ;
MainTest test = new MainTest ( ) ;
test. show ( ) ;
}
public void show ( ) {
}
}
class Main {
public static void main ( String[ ] args) {
for ( int i = 0 ; i < args. length; i++ ) {
args[ i] = "args_" + i;
System. out. println ( args[ i] ) ;
}
}
}
package com. atguigu. java2;
public class MainDemo {
public static void main ( String[ ] args) {
for ( int i = 0 ; i < args. length; i++ ) {
System. out. println ( "*****" + args[ i] ) ;
int num = Integer. parseInt ( args[ i] ) ;
System. out. println ( "#####" + num) ;
}
}
}
com.atguigu.java3
package com. atguigu. java3;
public class BlockTest {
public static void main ( String[ ] args) {
String desc = Person. desc;
System. out. println ( desc) ;
Person p1 = new Person ( ) ;
Person p2 = new Person ( ) ;
System. out. println ( p1. age) ;
Person. info ( ) ;
}
}
class Person {
String name;
int age;
static String desc = "我是一个人" ;
public Person ( ) {
}
public Person ( String name, int age) {
this . name = name;
this . age = age;
}
{
System. out. println ( "hello, block - 2" ) ;
}
{
System. out. println ( "hello, block - 1" ) ;
age = 1 ;
eat ( ) ;
desc = "我是一个爱学习的人1" ;
info ( ) ;
}
static {
System. out. println ( "hello,static block-2" ) ;
}
static {
System. out. println ( "hello,static block-1" ) ;
desc = "我是一个爱学习的人" ;
info ( ) ;
}
public void eat ( ) {
System. out. println ( "吃饭" ) ;
}
@Override
public String toString ( ) {
return "Person [name=" + name + ", age=" + age + "]" ;
}
public static void info ( ) {
System. out. println ( "我是一个快乐的人!" ) ;
}
}
package com. atguigu. java3;
class Root {
static {
System. out. println ( "Root的静态初始化块" ) ;
}
{
System. out. println ( "Root的普通初始化块" ) ;
}
public Root ( ) {
super ( ) ;
System. out. println ( "Root的无参数的构造器" ) ;
}
}
class Mid extends Root {
static {
System. out. println ( "Mid的静态初始化块" ) ;
}
{
System. out. println ( "Mid的普通初始化块" ) ;
}
public Mid ( ) {
super ( ) ;
System. out. println ( "Mid的无参数的构造器" ) ;
}
public Mid ( String msg) {
this ( ) ;
System. out. println ( "Mid的带参数构造器,其参数值:"
+ msg) ;
}
}
class Leaf extends Mid {
static {
System. out. println ( "Leaf的静态初始化块" ) ;
}
{
System. out. println ( "Leaf的普通初始化块" ) ;
}
public Leaf ( ) {
super ( "尚硅谷" ) ;
System. out. println ( "Leaf的构造器" ) ;
}
}
public class LeafTest {
public static void main ( String[ ] args) {
new Leaf ( ) ;
System. out. println ( ) ;
new Leaf ( ) ;
}
}
package com. atguigu. java3;
class Father {
static {
System. out. println ( "11111111111" ) ;
}
{
System. out. println ( "22222222222" ) ;
}
public Father ( ) {
System. out. println ( "33333333333" ) ;
}
}
public class Son extends Father {
static {
System. out. println ( "44444444444" ) ;
}
{
System. out. println ( "55555555555" ) ;
}
public Son ( ) {
System. out. println ( "66666666666" ) ;
}
public static void main ( String[ ] args) {
System. out. println ( "77777777777" ) ;
System. out. println ( "************************" ) ;
new Son ( ) ;
System. out. println ( "************************" ) ;
new Son ( ) ;
System. out. println ( "************************" ) ;
new Father ( ) ;
}
}
package com. atguigu. java3;
public class OrderTest {
public static void main ( String[ ] args) {
Order order = new Order ( ) ;
System. out. println ( order. orderId) ;
}
}
class Order {
int orderId = 3 ;
{
orderId = 4 ;
}
}
package com. atguigu. java3;
public class FinalTest {
final int WIDTH = 0 ;
final int LEFT;
final int RIGHT;
{
LEFT = 1 ;
}
public FinalTest ( ) {
RIGHT = 2 ;
}
public FinalTest ( int n) {
RIGHT = n;
}
public void doWidth ( ) {
}
public void show ( ) {
final int NUM = 10 ;
}
public void show ( final int num) {
System. out. println ( num) ;
}
public static void main ( String[ ] args) {
int num = 10 ;
num = num + 5 ;
FinalTest test = new FinalTest ( ) ;
test. show ( 10 ) ;
}
}
final class FinalA {
}
class AA {
public final void show ( ) {
}
}
class BB extends AA {
}