コンストラクタ以外で[[構造体]]や[[インスタンス]]を生成するメソッドのこと。構造体や[[インスタンス]]未生成の状態で利用する。
**[[TypeScript]]の例**
```ts
type DateStr = `${number}-${number}-${number}`;
class Date {
private constructor(
public year: number,
public month: number,
public day: number
) {}
static of(dateStr: DateStr): Date {
const [year, month, day] = dateStr.split("-").map(Number);
return new Date(year, month, day);
}
}
const d = Date.of("2023-10-16");
// ^? Date
console.log(`${d.year}年${d.month}月${d.day}日`);
// 2023年10月16日
```
**Rustの例**
```rust
struct Date {
year: i32,
month: i32,
day: i32,
}
impl Date {
fn new(date_str: &str) -> Date {
let strs = date_str
.split('-')
.map(|x| x.parse::<i32>().unwrap())
.collect::<Vec<_>>();
Date {
year: strs[0],
month: strs[1],
day: strs[2],
}
}
}
fn main() {
let d = Date::new("2023-10-16");
eprintln!("{:?}年{:?}月{:?}日", d.year, d.month, d.day);
// 2023年10月16日
}
```