package com.company;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
import sun.security.provider.ConfigFile;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Date;
import java.util.Random;
public class Main {
public static void main(String[] args) {
/* *//*java用false和true表达一切逻辑结果*//*
System.out.println(1 == 2);
System.out.println(1 == 1);//println这个方法表示输出之后,再在后面输出一个换行符
float[] f=new float[10];
Random random=new Random(47);
for (int i=0;i < 10;i++)
f[i]=random.nextFloat();
*//*for-in循环,常用于遍历数组和集合元素*//*
for(float x: f)//x相当于一个临时变量,暂存从f数组中扫描出来的结果
System.out.println(x);
for (char x: "abcdefg".toCharArray())
System.out.println(x);
char x=1;
PrimitiveOverloading primitiveOverloading=new PrimitiveOverloading(x);//一旦显式地定义了构造器,编译器就不会自动创建一个无参构造器。构造器解决了初始化的问题
primitiveOverloading.f1(x);
primitiveOverloading.increment().increment().increment();//在相同对象上执行多次操作
Person.eat(new Apple());
Flower flower=new Flower();
flower.print();
InitialValues initialValues=new InitialValues();
initialValues.print();
House house=new House();
house.f();*/
/* new Cupboard();
new Cupboard();
table.f2(1);
cupboard.f3(1);*/
// Cups.cup1.f(99);//没有生成Cups的对象,因此不会调用Cups的构造器
/*
new Mugs();
new Mugs(1);*/
/*// 数组初始化,下面两种都行,不过第二个更合理,因为第二个表示是一个对象
// int a[];
// int[] a;这里的a是对象的一个引用,但还没有new一个对象与其相关联,因此必须没有给数组分配存储空间。编译器不允许指定数组大小。
int[] a1={1,2,3,4,5};//第一种初始化,是特殊的初始化,只能在创建的时候引用,这时候编译器自动帮忙new出了一个对象
int[] a2;
a2=a1;//此时相当于复制了一个引用,此时a2,a1都是同一个对象的引用
for (int i=0;i<a2.length;i++){
a2[i]++;
}
for (int value : a1) {
System.out.println(value);
}*/
// //下面一段代码表示数组的创建是在运行时进行的
// int[] a;
// Random random=new Random(46);
// int k=random.nextInt(20);
// a = new int[k];
// System.out.println(a.length);
// System.out.println(k);
// Integer[] a={
// 1,2,
// 3,
// };//自动打包
// Integer[] a1={
// 1,2,
// 3,//最后面的逗号可以省略或者不省略。这一特性使得维护长列表更加容易
// };
// System.out.println(Arrays.toString(a));
//
// Spiciness howHot = Spiciness.HOT;
// System.out.println(howHot);//java自动给枚举类型生成了toString方法
// for (Spiciness s:Spiciness.values()){//.values()静态方法会返回一个数组
// System.out.println(s+" 顺序 "+ s.ordinal());//.ordinal返回enum常量的声明顺序
// }
// printArray(new Object[]{47,3.14,11,34});
// printArray(new Object[]{"one","two"});
// printArray(new Object[]{new A(),new A(),new A()});
// f(1,"123","12");
// f(5,"1");
// f(6);
// f1('a');
// f1();
// f2(1);
// f2(1,2,3,4);
f('a', 'b', 'c');
f(1);
f(0L);
}
// static Table table = new Table();
// static Cupboard cupboard = new Cupboard();
// Spiciness howHot = Spiciness.
//可变参数列表,基于object
// static void printArray(Object[] args){
// for (Object obj:args){
// System.out.print(obj+ " ");
// }
// System.out.println();
// }
// //基于String的可变参数列表
// static void f(int required,String... trailing){
// System.out.println("required:"+required+" ");
// for (String s: trailing){
// System.out.print(s+" ");
// }
// System.out.println();
// }
// //基于基本类型和自动装箱的可变参数列表
// static void f1(Character... args){
// System.out.print(args.getClass());
// System.out.println("length"+args.length);
// }
// static void f2(int... args){
// System.out.print(args.getClass());
// System.out.println("length" + args.length);
// }
//可变参数列表的重载,但当调用f()的时候编译器就不知道调用哪个了
static void f(Character... args){
System.out.println("Character");
}
static void f(Integer... args){
System.out.println("Integer");
}
static void f(Long... args){
System.out.println("Long");
}
}
/*
基本类型转化与重载结合的问题。测试方法:逐一注释每一个重载方法。首先会调用参数类型相同的重载函数,若没有参数类型相同的重载函数,则调用有范围大于该类型且最近的参数的重载函数
如int调用float而不会调用double。
若没有范围大于该类型参数的重载函数,编译器会报错,因为这样的话会造成数据损失
*/
class PrimitiveOverloading{
int i=0;
PrimitiveOverloading(char x){
}
void f1(char x){
System.out.println("char");
}
void f1(byte x){
System.out.println("byte");
}
void f1(int x){
System.out.println("int");
}
void f1(long x){
System.out.println("long");
}
void f1(float x){
System.out.println("float");
}
void f1(double x){
System.out.println("double");
}
void pick(){
}
void pit(){
this.pick();
pick();
//以上两句意思相同,this表示引用当前的对象,比如创建的对象是a,this表示的就是a,创建的对象是b,this表示的就是b。
// 但是在类内部方法间调用的时候可以不写this,编译器自动做好了这些
}
PrimitiveOverloading increment(){
i++;
return this;//this可应用于这种情况,返回对当前对象的引用。这里,调用一次该函数,当前对象的i就++一次
}
}
//this也用于把当前对象传给外部方法
class Apple{
Apple getPeeled(){
return Peeler.peel(this);/*Apple处于某些原因,必须调用一个外部工具方法做一些行为,必须使用this才能将自身传递给外部方法*/
}
}
class Peeler{
static Apple peel(Apple apple){
return apple;
}
}
class Person{
public static void eat(Apple apple){
Apple peeled = apple.getPeeled();
System.out.println("Yummy");
}
}
class Flower{
int petalCount = 0;
String s="initial value";
Flower(int petal){
petalCount=petal;
}
Flower(String ss){
s=ss;
}
Flower(String s,int petals){
this(petals);//构造器中调用构造器
//this(s);只能调用一次,如果调用两个构造器就会报错
this.s=s;/*this的另外一种用法,参数中的s和成员变量s相同,会引起混淆,用this.s可以避免这种情况*/
// this(petals);必须先调用构造器,否则会报错
}
Flower(){
this("hi",47);
}
void print(){
// this(1);编译器不允许在一个构造器之外的方法里面调用构造器
System.out.println(s+" "+petalCount);
}
static void ppp(){
// print();静态方法是为类而创建的,不需要任何对象,因此不能调用非静态方法。 `
// this.s=" ";也不会存在this
}
void f(){
/*
int i;
i++;
这样会报错,强制程序员初始化*/
}
}
class Book{
boolean checkOut=false;
Book(boolean checkOut){
this.checkOut=checkOut;
}
void checkIn(){
checkOut=false;
}
@Override
protected void finalize() throws Throwable {
if (checkOut){
System.out.println("checked out");
}
}
}
class InitialValues{
InitialValues initialValues;//若是成员变量是个对象,且没有被初始化,那么它就会被初始化为null
int i=f();//调用方法来赋初值
int j=2;//直接赋初值
int f(){
return 1;
}
void print(){
System.out.println(initialValues);
}
/*以上赋初值的方法有个限制,这个类的的每一个对象有相同的初值,有时需要更大的灵活性
* 更大的灵活性可以使用构造器方法*/
}
class Window{
Window(int marker){
System.out.println("window"+marker);
}
}
class House{//在类中变量定义的顺序决定了他们初始化的顺序,即使变量定义散布在方法定义之间,他们仍然会在任何方法(包括构造器)被调用之前得到初始化
Window w1=new Window(1);
House(){
System.out.println("House");
w3=new Window(33);//重新初始化w3
}
Window w2=new Window(2);
void f(){
System.out.println("f()");
}
Window w3=new Window(3);
}
/*以下是关于静态成员初始化
* 首先,静态成员只有必要的时候才会进行初始化,如果不创建Table对象,也不引用Table中的bowl1,bowl2,那么静态的Bowl类对象bowl1,bowl2永远不会被创建、
* 静态对象只会被初始化一次
* 初始化顺序: 静态对象 非静态对象 构造器
* 创建对象的过程,以Dog类为例: 1,首次创建Dog类时,先定位到Dog.class 2,加载完Dog.class之后,所有的静态初始化的动作都会完成
* 3,用new Dog()创建对象是,首先会在堆上为Dog对象分配足够的空间 4,对象初始化 5,执行构造器*/
class Bowl{
Bowl(int marker){
System.out.println("Bowl" +marker);
}
void f1(int marker){
System.out.println("f1 "+marker);
}
}
class Table{
static Bowl bowl1 = new Bowl(1);
Table(){
System.out.println("Table()");
bowl1.f1(1);
}
void f2(int marker){
System.out.println("f2 "+marker);
}
static Bowl bowl2=new Bowl(2);
}
class Cupboard{
Bowl bowl3=new Bowl(3);
static Bowl bowl4 = new Bowl(4);
Cupboard(){
System.out.println("Cupboard()");
bowl4.f1(2);
}
void f3(int marker){
System.out.println("f3 " + marker);
}
static Bowl bowl5 = new Bowl(5);
}
/*显式的静态初始化*/
class Cup{
Cup(int marker){
System.out.println("Cup" + marker);
}
void f(int marker){
System.out.println("f "+marker);
}
}
class Cups{
static Cup cup1;
static Cup cup2;
static {/*显式的静态代码块,和静态成员相同,只会执行一次,执行顺序也是一样*/
cup1 = new Cup(1);
cup2 = new Cup(2);
}
Cups(){
System.out.println("Cups()");
}
}
//非静态实例初始化
class Mug{
Mug(int marker){
System.out.println("Mug "+marker);
}
}
class Mugs{
Mug mug1;
Mug mug2;
{/*和非静态成员相同,执行顺序也一样,用于支持匿名内部类*/
mug1 = new Mug(1);
mug2 = new Mug(2);
System.out.println("mug1 & mug2 initialized");
}
Mugs(){
System.out.println("Mugs");
}
Mugs(int i){
System.out.println("Mugs(int)");
}
}
//枚举类型,常用于switch语句中。enum是类,且有自己的方法
enum Spiciness{
NOT,MILD,MEDIUM,HOT,FLAMING
}
//可变参数列表
class A{
}