把精力放在java基础,设计模式,jvm原理,spring+springmvc原理及源码,linux,mysql事务隔离与锁机制,mongodb,http/tcp,多线程,分布式架构,弹性计算架构,微服务架构,java性能优化,以及相关的项目管理等等
1、import java.io.*;
public class Employee{
String name;
int age;
String designation;
double salary;
// Employee 类的构造器, //必须带参数
public Employee(String name){ //公共变量,对所有类可见,private只对此类可见
this.name = name;
}
// 设置age的值
public void empAge(int empAge){
age = empAge;
}
/* 设置designation的值*/
public void empDesignation(String empDesig){
designation = empDesig;
}
/* 设置salary的值*/
public void empSalary(double empSalary){
salary = empSalary;
}
/* 打印信息 */
public void printEmployee(){
System.out.println("名字:"+ name );
System.out.println("年龄:" + age );
System.out.println("职位:" + designation );
System.out.println("薪水:" + salary);
}
}
2、静态变量
- 类变量也称为静态变量,在类中以static关键字声明,但必须在方法构造方法和语句块之外。
- 静态变量在第一次被访问时创建,在程序结束时销毁。
类变量被声明为public static final类型时,类变量名称一般建议使用大写字母
public class StaticDepart {
private static double salary;
public static final String DEPARTMENT = "开发人员";
public static void main(String[] args) {
salary = 1000;
System.out.println(DEPARTMENT + salary);
}
}
3、修饰符
-
default (即缺省,什么也不写): 在同一包内可见,不使用任何修饰符。使用对象:类、接口、变量、方法。
-
private : 在同一类内可见。使用对象:变量、方法。 注意:不能修饰类(外部类)
-
public : 对所有类可见。使用对象:类、接口、变量、方法
-
protected : 对同一包内的类和所有子类可见。使用对象:变量、方法。 注意:不能修饰类(外部类)。
4、
class Vehicle {};
public class Final_test extends Vehicle { //Final_test 继承Vehicle
public static void main(String[] args){
Vehicle a = new Final_test();
boolean result = a instanceof Final_test; //boolean 布尔值
System.out.println( result);
}
}
5、
public class InstanceCounter {
private static int numInstances = 0;
protected static int getCount() { //有整数返回值
return numInstances;
}
private static void addInstance() { //无返回值
numInstances++;
}
InstanceCounter() {
InstanceCounter.addInstance();
}
public static void main(String[] args) {
System.out.println(InstanceCounter.getCount());
for (int i = 0; i < 100; i++) {
new InstanceCounter();
}
System.out.println(InstanceCounter.getCount());
}
}
6、for循环
public class ForTest {
public static void main(String args[]) {
int[] numbers = {1,2,3,5,3}; //定义列表
for (int i : numbers) {
System.out.println(i);
}
}
}
7、
public class Test{
public static void main(String args[]){
StringBuffer sBuffer = new StringBuffer("菜鸟教程官网:"); //字符串变换、操作 用StringBuffer
sBuffer.append("www");
sBuffer.append(".runoob");
sBuffer.append(".com"); System.out.println(sBuffer); }
}
8、 获取当前时间
import java.util.*;
import java.text.*;
public class DateText {
public static void main(String args[]) {
// 初始化 Date 对象
Date date = new Date();
SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss"); //时间占位
// 使用 toString() 函数显示日期时间
System.out.println(ft.format(date));
}
}
9、
import java.util.*;
import java.text.*;
public class DateText {
public static void main(String args[]) {
SimpleDateFormat ft = new SimpleDateFormat("yyyy-mm-dd");
String input = args.length == 0?"1813-11-11" :args[0]; //是的话执行前面
System.out.print(input);
Date t;
try {
t = ft.parse(input);
System.out.println(t);
} catch (ParseException e) {
// TODO: handle exception
System.out.println(ft);
}
}
}
10、正则
import java.util.regex.*; //导入正则包
class RegexExample1{
public static void main(String args[]){
String content = "I am noob " + "from runoob.com.";
String pattern = ".*runoob.*"; //.表示任意字符
boolean isMatch = Pattern.matches(pattern, content); //true
System.out.println("字符串中是否包含了 'runoob' 子字符串? " + isMatch);
} }
11、
public class RegexMatches {
private static String REGEX = "a*b";
private static String INPUT = "aabfooaabfooabfoobkkk";
private static String REPLACE = "-";
public static void main(String[] args) {
Pattern p = Pattern.compile(REGEX);
// 获取 matcher 对象
Matcher m = p.matcher(INPUT);
StringBuffer sb = new StringBuffer();
while(m.find()){
m.appendReplacement(sb,REPLACE);
System.out.println(sb.toString());
}
m.appendTail(sb); //将最后添加
System.out.println(sb.toString());
}
}
结果:
-
-foo-
-foo-foo-
-foo-foo-foo-
-foo-foo-foo-kkk
12、
//一个简单的构造函数
class MyClass {
int x;
// 以下是构造函数
MyClass(int i ) {
x = i;
}
}
public class ConsDemo {
public static void main(String args[]) {
MyClass t1 = new MyClass( 10 );
MyClass t2 = new MyClass( 20 );
System.out.println(t1.x + " " + t2.x);
}
}
4、将各歌曲合并一个串烧。
剪切。
public class MusicCompound
{
public static void main(String args[])
{
FileOutputStream fileOutputStream = null;
FileInputStream fileInputStream = null;
String fileNames[] = {"E:/星月神话.mp3","E:/我只在乎你.mp3"};
//设置byte数组,每次往输出流中传入8K的内容
byte by[] = new byte[1024*8];
try
{
fileOutputStream = new FileOutputStream("E:/合并.mp3"); //定义输出的文件
for(int i=0;i<2;i++)
{
int count = 0;
fileInputStream = new FileInputStream(fileNames[i]); //输入文件
//跳过前面3M的歌曲内容
fileInputStream.skip(1024*1024*3);
while(fileInputStream.read(by) != -1) //读完了,就返回-1
{
fileOutputStream.write(by); //写入
count++;
System.out.println(count);
//要截取中间2MB的内容,每次输入8k的内容,所以输入的次数是1024*2/8
if(count == (1024*2/8))
{
break;
}
}
}
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
try
{
//输出完成后关闭输入输出流
fileInputStream.close();
fileOutputStream.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}
5、类 属性、方法
public class Penguin {
private String name;
private int id;
public Penguin (String myName, int myid) {
name = myName;
id = myid;
}
public void eat() {
System.out.println('吃');
}
}
6、多态
创建Employe类
package exam_test2;
public class Employe {
private String name;
private String address;
private int number;
public Employe(String name, String address, int number) {
this.name = name; //属性
this.address = address;
this.number = number;
}
public void mailCheck() { //方法
System.out.print("邮寄给"+this.name+" "+this.address);
}
public String toString() {
return name+address+number;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public void setAddress(String newAddress) {
address = newAddress;
}
public int getNumber() {
return number;
}
}
package exam_test2;
public class Salary extends Employe //继承Employe
{
private double salary; // 全年工资
public Salary(String name, String address, int number, double salary) {
super(name, address, number); //父类
setSalary(salary); //自己
}
public void mailCheck() {
System.out.println("Salary 类的 mailCheck 方法 ");
System.out.println("邮寄支票给:" + getName()
+ " ,工资为:" + salary);
}
public double getSalary() {
return salary;
}
public void setSalary(double newSalary) {
if(newSalary >= 0.0) {
salary = newSalary;
}
}
public double computePay() {
System.out.println("计算工资,付给:" + getName());
return salary/52;
}
}
调用
package exam_test2;
public class VirtualDemo {
public static void main(String [] args) {
Salary s = new Salary("员工 A", "北京", 3, 3600.00);
Employe e = new Salary("员工 B", "上海", 2, 2400.00);
System.out.println("使用 Salary 的引用调用 mailCheck -- ");
s.mailCheck();
System.out.println("\n使用 Employee 的引用调用 mailCheck--");
e.mailCheck();
}
}
7、抽象类
不可以直接实例化,需要通过子类来继承实现。
8、抽象方法
也是需要通过子类来确定。
public abstract class Employee {
private String name; private String address;
private int number;
public abstract double computePay(); //没有花括号
//其余代码 }
通过子类实现
public class Salary extends Employee {
private double salary; // Annual salary
public double computePay() {
System.out.println("Computing salary pay for " + getName()); return salary/52; }
//其余代码 }
9、封装
public class Person{
private String name;
private int age; //私有变量
public int getAge(){
return age;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age = age; //通过公共方法访问
} public void setName(String name){
this.name = name;
}
}
10、接口,可以多继承
- 接口是隐式抽象的,当声明一个接口的时候,不必使用abstract关键字。
- 接口中每一个方法也是隐式抽象的,声明时同样不需要abstract关键字。
- 接口中的方法都是公有的。
定义:
public interface Animal_interface { //interface
public void eat();
public void travel();
}
实现:
public class MammalInt implements Animal_interface{ //implements
public void eat(){
System.out.println("Mammal eats");
}
public void travel(){
System.out.println("Mammal travels");
}
public int noOfLegs(){
return 0;
}
public static void main(String args[]){
MammalInt m = new MammalInt();
m.eat();
m.travel();
}
}
数据结构:
1、枚举,被迭代器取代
import java.util.Enumeration;
import java.util.Vector;
public class EnumerationTester { //枚举,被迭代器取代
public static void main(String args[]) {
Enumeration<String> days;
Vector<String> dayNames = new Vector<String>();
dayNames.add("Sunday");
dayNames.add("Monday");
dayNames.add("Tuesday");
dayNames.add("Wednesday");
dayNames.add("Thursday");
dayNames.add("Friday");
dayNames.add("Saturday");
days = dayNames.elements();
while (days.hasMoreElements()){
System.out.println(days.nextElement());
}
}
}
2、栈
import java.util.*;
public class StackDemo {
static void showpush(Stack<Integer> st,int a){ //静态
st.push(new Integer(a));
System.out.println("push(" + a + ")");
System.out.println("stack: " + st);
}
static void showpop(Stack<Integer> st) { //后进先出
System.out.print("pop -> ");
Integer a = (Integer) st.pop(); //赋值,类型带上
System.out.println(a);
System.out.println("stack: " + st);
}
public static void main(String args[]) {
Stack<Integer> st = new Stack<Integer>();
System.out.println("stack: " + st);
showpush(st, 42);
showpush(st, 66);
showpush(st, 99);
showpop(st);
showpop(st);
showpop(st);
try {
showpop(st); //空了
} catch (EmptyStackException e) {
System.out.println("empty stack");
}
}
}
3、哈希
import java.util.*;
//import com.sun.org.apache.bcel.internal.generic.NEW;
public class HashTableDemo {
public static void main(String args[]) {
// Create a hash map
Hashtable balance = new Hashtable();
Enumeration names;
String str;
double bal;
balance.put("Zara", new Double(3434.34));
balance.put("Mahnaz", new Double(123.22));
balance.put("Ayan", new Double(1378.00));
balance.put("Daisy", new Double(99.22));
balance.put("Qadir", new Double(-19.08));
// Show all balances in hash table.
names = balance.keys();
while(names.hasMoreElements()) {
str = (String) names.nextElement();
System.out.println(str + ": " +
balance.get(str));
}
bal = ((Double)balance.get("Zara")).doubleValue();
balance.put("Zara", new Double(bal+1000));
System.out.println("Zara's new balance: " +
balance.get("Zara"));
}}
4、import java.util.*;
public class Test{
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("1", "value1");
map.put("2", "value2");
map.put("3", "value3");
//第一种:普遍使用,二次取值
System.out.println("通过Map.keySet遍历key和value:");
for (String key : map.keySet()) {
System.out.println("key= "+ key + " and value= " + map.get(key));
}
//第二种
System.out.println("通过Map.entrySet使用iterator遍历key和value:");
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
//第三种:推荐,尤其是容量大时
System.out.println("通过Map.entrySet遍历key和value");
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
//第四种
System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
for (String v : map.values()) {
System.out.println("value= " + v); }
} }
5、泛型
public class Box<T> {
private T t;
public void add(T t) {
this.t = t;
}
public T get() {
return t;
}
public static void main(String[] args) {
Box<Integer> integerBox = new Box<Integer>();
Box<String> stringBox = new Box<String>();
// Box<Integer> integerBox = new Box<Integer>();
integerBox.add(new Integer(10));
stringBox.add(new String("10"));
System.out.println(integerBox.get());
System.out.println(integerBox.get());
}
}
6、
class RunnableDemo implements Runnable { //线程
private Thread t;
private String threadName;
RunnableDemo( String name) { //添加参数
threadName = name;
System.out.println("Creating " + threadName );
}
public void run() {
System.out.println("Running " + threadName );
try {
for(int i = 4; i > 0; i--) {
System.out.println("Thread: " + threadName + ", " + i);
// 让线程睡眠一会
Thread.sleep(50);
}
}catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted.");
}
System.out.println("Thread " + threadName + " exiting.");
}
public void start () {
System.out.println("Starting " + threadName );
if (t == null) {
t = new Thread (this, threadName); //创建线程
t.start (); //执行start,在执行run
}
}
}
public class TestThread {
public static void main(String args[]) {
RunnableDemo R1 = new RunnableDemo( "Thread-1");
R1.start();
RunnableDemo R2 = new RunnableDemo( "Thread-2");
R2.start();
}
}