Static methods can call the main() class BL directly through the class name without using the new object
Non-static methods cannot call getAA() directly by class name.
If a static method calls a non-static method
BL.main()
BL. getAA ()* Error
It is proved that static methods cannot call non-static methods.
Assumption: Static methods can call non-static methods
Class BL
Static method main () ---"getAA()
Non-static method getAA()
BL.main() —>getAA()
BL. getAA () error
Fighting landlords after revision
package com.zhongruan; import java.util.*; public class Doudiz { public static void main(String[] args){ //1. Prepare a deck of cards Map<Integer,String> poker=new HashMap<>(); String[] nums={"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; String[] colors={"Spade","Red heart","Plum blossom","block"}; int index=0; for (String num:nums){ for (String color:colors){ String pai=color+num; index=index+1; poker.put(index,pai); } } System.out.println(poker); index++; poker.put(index,"king"); index++; poker.put(index,"Xiao Wang"); System.out.println(poker); //2. shuffle List<Integer> pokerIndexs=new ArrayList<>(); Set<Integer> integers=poker.keySet(); for (Integer i:poker.keySet()){ pokerIndexs.add(i); } Collections.shuffle(pokerIndexs); System.out.println(pokerIndexs); //3. leave 3 cards. Set<Integer> dipaiInds=new TreeSet<>(); dipaiInds.add(pokerIndexs.remove(0)); dipaiInds.add(pokerIndexs.remove(0)); dipaiInds.add(pokerIndexs.remove(0)); System.out.println(dipaiInds); System.out.println(pokerIndexs); //4. licensing Set<Integer> xz=new TreeSet<>(); Set<Integer> wyb=new TreeSet<>(); Set<Integer> wy=new TreeSet<>(); for (int i=0;i<pokerIndexs.size();i++){ int pi=pokerIndexs.get(i); int mod=i%3; if (mod==0){ xz.add(pi); }else if (mod==1){ wyb.add(pi); }else { wy.add(pi); } } System.out.println(xz); System.out.println(wyb); System.out.println(wy); //5. look at cards look(poker,xz); look(poker,wyb); look(poker,wy); } public static void look(Map<Integer,String>poker,Set<Integer> indexs){ List<String> p=new ArrayList<>(); for(Integer i:indexs){ String pai=poker.get(i); p.add(pai); } System.out.println(p); } }
2. Music Manager
First create a music project, create a lib directory under the music directory, and import the jar package
Create com package under src directory, zhongruan package under com package, model package and Util package under zhongruan package
Create Music under model and DBUtil in Util
The code in the Test file is as follows:
package com.zhongruan; import com.zhongruan.Util.DBUtil; import com.zhongruan.model.Music; import java.awt.*; import java.awt.image.DirectColorModel; import java.sql.*; import java.util.ArrayList; import java.util.List; public class Test { public List<Music> findMusic(){ ResultSet resultSet=null; PreparedStatement statement=null; Connection connection=null; List<Music> musics=new ArrayList<>(); try { connection= DBUtil.getConnection(); String sql="select * from music"; //4. Get the statement statement=connection.prepareStatement(sql); //5. implementation of sql resultSet=statement.executeQuery(); //6. Processing result sets while (resultSet.next()){ Music music=new Music(); music.setId(resultSet.getInt(1)); music.setName(resultSet.getString(2)); music.setAuthor(resultSet.getString(3)); musics.add(music); } //7. Closing resources }catch (Exception e){ e.printStackTrace(); }finally { DBUtil.closeAll(resultSet,statement,connection); } return musics; } public static void main(String[] args){ Test test=new Test(); List<Music> musics=test.findMusic(); System.out.println(musics); } }
The DBUtil file code is as follows:
package com.zhongruan.Util; import java.sql.*; public class DBUtil { public static Connection getConnection() throws ClassNotFoundException, SQLException { //1. Load Driver Class.forName("com.mysql.jdbc.Driver"); //2. Create connections Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/zjgm?characterEncoding=utf-8&user=root&password=123456"); return connection; } public static void closeAll(ResultSet resultSet,Statement statement,Connection connection){ if (resultSet!=null){ try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } if (statement!=null){ try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (connection!=null){ try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
The Music file code is as follows:
package com.zhongruan.model; public class Music { private int id; private String name; private String author; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } @Override public String toString() { return "Music{" + "id=" + id + ", name='" + name + '\'' + ", author='" + author + '\'' + '}'; } }