一、Java基礎
1、Java OOP的概念
Java是一門面向對象的編程語言,OOP(Object Oriented Programming)即面向對象編程,是Java的基礎。Java的OOP的特點包括:封裝、繼承、多態。
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
}
2、Java異常處理
Java的異常處理包括try-catch、throw和throws。try-catch語句用於捕獲異常,throw用於手動拋出異常,throws用於聲明可能拋出的異常。
try {
//可能拋出異常的代碼
} catch (Exception e) {
//捕獲異常
}
3、Java集合框架
Java的集合框架包括List、Set、Map等。List接口是有序集合,Set接口是不重複集合,Map接口是鍵值對映射集合。
List list = new ArrayList();
list.add("Java");
list.add("Python");
list.add("C++");
Set set = new HashSet();
set.add(1);
set.add(2);
set.add(3);
Map map = new HashMap();
map.put("Java", 1);
map.put("Python", 2);
map.put("C++", 3);
二、Java高級
1、Java線程
Java的多線程是Java高級編程的一個重要方面,實現多線程可以提高程序的運行效率。Java的線程有兩種方式:繼承Thread類和實現Runnable接口。
public class MyThread extends Thread {
public void run() {
//實現多線程的代碼
}
}
public class MyRunnable implements Runnable {
public void run() {
//實現多線程的代碼
}
}
//使用兩種方式創建線程
MyThread myThread = new MyThread();
Thread myRunnable = new Thread(new MyRunnable());
2、Java反射
Java反射是Java高級開發的一個重要方面,Java允許在程序運行時動態獲取類的信息,並且可以調用類的方法。Java反射包括獲取Class對象、生成對象、調用方法等操作。
public class Person {
public void sayHello(String name) {
System.out.println("Hello, " + name);
}
}
//獲取Class對象
Class personClass = Person.class;
//生成對象
Person person = personClass.newInstance();
//調用方法
Method sayHelloMethod = personClass.getMethod("sayHello", String.class);
sayHelloMethod.invoke(person, "Java");
3、Java註解
Java註解是Java高級開發的一個重要方面,可以為程序添加元數據。Java的註解包括元註解和自定義註解,元註解是註解的註解。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value();
}
//使用自定義註解
@MyAnnotation("Java")
public class Person {
}
三、Java框架
1、Spring框架
Spring框架是Java的一個開源框架,可以用於創建Web應用程序和企業級應用程序。Spring框架的核心是IoC(控制反轉)和AOP(面向切面編程)。
//注入依賴
@Service
public class PersonService {
private PersonDao personDao;
@Autowired
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
}
//控制事務
@Transactional
public void savePerson(Person person) {
personDao.savePerson(person);
}
2、MyBatis框架
MyBatis框架是Java的一個持久化框架,可以用於將Java對象映射到數據庫表中。MyBatis框架的核心是SqlSession和Mapper接口。
//定義Mapper接口
public interface PersonMapper {
Person selectById(int id);
}
//使用Mapper接口
SqlSession sqlSession = sqlSessionFactory.openSession();
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
Person person = personMapper.selectById(1);
3、Spring Boot框架
Spring Boot框架是Spring的一個子項目,簡化了Spring應用程序的開發和部署。Spring Boot的核心是自動配置和起步依賴。
//啟動Spring Boot應用程序
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
//使用自動配置
@RestController
public class MyController {
@GetMapping("/hello")
public String hello() {
return "Hello, Spring Boot";
}
}
原創文章,作者:HRKG,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/132381.html
微信掃一掃
支付寶掃一掃