文章目录
The JAVA API(Built-in Packages)
1.The Java API is a library of prewritten classes, that are free to use, included in the Java Development Environment.
2.The library contains components for managing input, database programming, and much much more.
The complete list can be found at Oracles website:https://docs.oracle.com/javase/8/docs/api/
3.The library is divided into packages and classes. Meaning you can either import a single class (along with its methods and attributes), or a whole package that contain all the classes that belong to the specified package.
4.To use a class or a package from the library, you need to use the import
keyword:
import package.name.Class; // Import a single class
import package.name.*; // Import the whole package
-d <目录> 指定放置生成的类文件的位置
Inner Class
public class Out {
int x=10;
public void out(){
System.out.println("OUt work!");
}
public class In{
int y=20;
public void in(){
System.out.println("In work!");
}
}
}
public class Main {
public static void main(String[] args) {
Out myout = new Out();
Out.In myin = myout.new In();
myin = = new Out().new In();
左右两边完全相等
myout.out();
myin.in();
System.out.println(myout.x + myin.y);
}
}
运行结果:
OUt work!
In work!
30
在继承关系下,关于一次性创建(new)多个类(感觉上像数组),并且结合使用增强for循环进行遍历
public abstract class Animal {
public abstract void Cry();
}
关于abstract修饰词的使用
强调了在父类中先定义(不写任何内容),但在子类中必须Override此方法
public class Cat extends Animal{
public void Cry(){
System.out.println("喵喵喵");
}
}
public class Dog extends Animal{
public void Cry(){
System.out.println("汪汪汪");
}
}
public class Main {
public static void main(String[] args) {
Animal[] animals = new Animal[2];
animals[0] = new Dog();
animals[1] = new Cat();
for (Animal animal : animals) {
animal.Cry();s
}
}
}
增强fo循环:
可以简化数组和集合的遍历操作,animal是一个临时变量,用于存放数组中的元素,每次循环都会将数组中的下一个元素赋值给它,直到遍历完整个数组为止。
for (int i = 0; i < animals.length; i++) {
animals[i].cry();
}
这样写也可以达到同样的效果,但是相对来说代码会比较繁琐,特别是当数组中元素较多时。增强for循环的语法结构比较简单,使用起来也比较方便,所以在实际开发中更为常见。
interface
interface Animal{
public void sleep();
public void eat();
}
interface Zookeeper {
public void feed();
}
class Cat implements Animal ,Zookeeper{
public void sleep(){
System.out.println("小猫睡觉");
}
public void eat(){
System.out.println("小猫吃鱼");
}
public void feed(){
System.out.println("喂小猫鱼");
}
}
继承只能单继承,而可以实现多个接口
public class Main {
public static void main(String[] args) {
Cat cat = new Cat();
cat.sleep();cat.eat();cat.feed();
}
}
运行结果:
小猫睡觉
小猫吃鱼
喂小猫鱼
Enum(枚举)的使用
public enum Level {
low,
medium,
high
}
public class Main {
public static void main(String[] args) {
Level mylevel = Level.high;
switch(mylevel){
case low:
System.out.println("Low level");
break;
case medium:
System.out.println("Medium level");
break;
case high:
System.out.println("High level");
break;
}
//The enum type has a values() method, which returns an array of all enum constants. This method is useful when you want to loop through the constants of an enum:
for(Level Mylevel : Level.values()){
System.out.println(Mylevel);
}
}
}
computational results:
High level
low
medium
high
Dates and Time
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
LocalDate mydate = LocalDate.now();
LocalTime mytime = LocalTime.now();
LocalDateTime mydatetime = LocalDateTime.now();
System.out.println(mydate);
System.out.println(mytime);
System.out.println(mydatetime);
DateTimeFormatter myformat = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String formattedDate = mydatetime.format(myformat);
System.out.println("After formatting: " + formattedDate);
//格式化输出
}
}
computational results:
2023-04-25
19:54:54.961
2023-04-25T19:54:54.961
After formatting: 25-04-2023 19:54:54
ArrayList
import java.util.ArrayList;
import java.util.Collections;
public class cars {
public static void main(String[] args) {
ArrayList<String> Cars = new ArrayList<String>();
Cars.add("BMW");
Cars.add("Ford");
Cars.add("0000");//To add elements
System.out.println(Cars);
String myDremacar = Cars.get(0);//To acess an element
System.out.println("myDreancar:" + myDremacar);
Cars.set(2,"Volvo");//To modify an element
System.out.println(Cars.get(2));
Cars.remove(1);
System.out.println(Cars);//To remove an element
System.out.println(Cars.size());
for(int i=0; i< Cars.size();i++)
System.out.println(Cars.get(i));
for(String i : Cars)
System.out.println(i);
Cars.clear();//To remove all the elements in the ArrayList
System.out.println(Cars);
}
}
computational results:
[BMW, Ford, 0000]
myDreancar:BMW
Volvo
[BMW, Volvo]
2
BMW
Volvo
BMW
Volvo
[]
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
public class mysort {
public static void main(String[] args) {
ArrayList<String> mylist = new ArrayList<String>();
ArrayList<Integer> mynumber = new ArrayList<Integer>();
mylist.add("ba");
mylist.add("ab");
mylist.add("aa");
System.out.println(mylist);
Collections.sort(mylist);
System.out.println(mylist);
mynumber.add(32);
mynumber.add(12);
mynumber.add(9);
System.out.println(mynumber);
Collections.sort(mynumber);
System.out.println(mynumber);
}
}
computational results:
[ba, ab, aa]
[aa, ab, ba]
[32, 12, 9]
[9, 12, 32]
Linkedlist
import java.util.LinkedList;
public class Car {
public static void main(String[] args) {
LinkedList<String> car = new LinkedList<String>();
car.add("BWM");
car.add("Volvo");
car.add("Audi");
car.add("红旗");
System.out.println(car.get(2));
car.addLast("0000");
System.out.println(car.get(4));
}
}
computerational:
Audi
0000
HashMap
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<String, String> capitalCities = new HashMap<String, String>();
capitalCities.put("England", "London");
capitalCities.put("Germany", "Berlin");
capitalCities.put("Norway", "Oslo");
capitalCities.put("USA", "Washington DC");
for (String i : capitalCities.keySet()) {
System.out.println("key: " + i + " value: " + capitalCities.get(i));
}
String my = capitalCities.get("England");
System.out.println(capitalCities.get("England"));
if(my.equals(capitalCities.get("England"))){
System.out.println("This is true.");
}
}
}
computerational
key: USA value: Washington DC
key: Norway value: Oslo
key: England value: London
key: Germany value: Berlin
London
This is true.
Lambda表达式—简化对函数接口的实现
Lambda表达式是Java 8中引入的一种新语法,可以用来简化对函数接口的实现。
举个例子,假设你有一个接口Calculator,它只有一个计算方法add:
interface Calculator {
int add(int a, int b);
}
在Java 8之前,如果你要实现这个接口的方法,你要这样写:
Calculator calculator = new Calculator() {
@Override
public int add(int a, int b) {
return a + b;
}
};
而使用Lambda表达式,你可以这样:
Calculator calculator = (a, b) -> a + b;
其中,(a, b)是方法的参数,箭头后面的表达式a + b是方法的实现。
Lambda表达式实现了函数式编程的概念,可以使代码更加简洁易懂。在Java 8中,许多标准库已经使用Lambda表达式重写,例如Stream API。
简便理解:
public interface math {
int add(int a,int b);
}
函数接口--->单一接口
public class Main {
public static void main(String[] args) {
math n = new math() {
@Override
public int add(int a, int b) {
return a+b;
}
};
//匿名内部类方式实现
math m = (a,b) -> a+b;
//Lambda表达式方式简化实现
System.out.println(n.add(2, 3));
System.out.println(m.add(1, 2));
}
}
lic class Main {
public static void main(String[] args) {
math n = new math() {
@Override
public int add(int a, int b) {
return a+b;
}
};
//匿名内部类方式实现
math m = (a,b) -> a+b;
//Lambda表达式方式简化实现
System.out.println(n.add(2, 3));
System.out.println(m.add(1, 2));
}
}