การสืบทอดและอินเทอร์เฟซ
ใช้การสืบทอดเพื่อให้โค้ดนำกลับมาใช้ซ้ำได้ และกำหนด API ของสัญญาให้ชัดเจนด้วยอินเทอร์เฟซ เพื่อการแบ่งส่วนโมดูลที่ดียิ่งขึ้น
การสืบทอดและอินเทอร์เฟซ เป็นบทเรียน Blockchain Smart Contracts with Solidity ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Blockchain Smart Contracts with Solidity และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Blockchain Smart Contracts with Solidity มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What is Inheritance?
In Solidity, inheritance allows one smart contract (the child) to reuse code, functions, and state variables from another contract (the parent). This is a fundamental concept in object-oriented programming.
- It promotes code reusability.
- It helps organize complex contract logic.
- It allows for modular and extensible designs.
Basic Inheritance Syntax
To make a contract inherit from another, you use the is keyword. The child contract gets access to all public and internal members of the parent contract.
Think of it like a child inheriting traits from its parent.
pragma solidity ^0.8.0;
contract ParentContract {
uint public parentValue;
constructor() {
parentValue = 100;
}
function getParentValue() public view returns (uint) {
return parentValue;
}
}
contract ChildContract is ParentContract {
// ChildContract inherits parentValue and getParentValue()
}Inheriting Functions & State
When ChildContract inherits from ParentContract, it automatically gains access to parentValue and the getParentValue() function. You can directly interact with them from the child contract.
Let's deploy ChildContract and see its parent's value!
pragma solidity ^0.8.0;
contract ParentContract {
uint public parentValue;
constructor() {
parentValue = 100;
}
function getParentValue() public view returns (uint) {
return parentValue;
}
}
contract ChildContract is ParentContract {
function getMyParentValue() public view returns (uint) {
return getParentValue(); // Calling an inherited function
}
function getDirectParentValue() public view returns (uint) {
return parentValue; // Accessing an inherited state variable
}
}Overriding Functions
Sometimes, a child contract needs to provide its own implementation for a function that already exists in the parent. This is called overriding.
To override a function:
- The parent function must be marked with
virtual. - The child function must be marked with
override. - Function signatures (name, parameters, return types) must match exactly.
Override Example
Here, the ChildContract provides a new behavior for greet(). Notice the virtual and override keywords.
pragma solidity ^0.8.0;
contract GreeterParent {
function greet() public virtual view returns (string memory) {
return "Hello from Parent!";
}
}
contract GreeterChild is GreeterParent {
function greet() public override view returns (string memory) {
return "Hello from Child!";
}
}Calling Parent Functions with 'super'
When you override a function, you don't always have to completely replace its logic. You can still call the parent's version of the function within the overriding function using super.
This is useful for extending or modifying parent behavior instead of replacing it entirely.
pragma solidity ^0.8.0;
contract Base {
function foo() public virtual pure returns (string memory) {
return "Base foo";
}
}
contract Derived is Base {
function foo() public override pure returns (string memory) {
string memory baseMessage = super.foo(); // Call parent's foo()
return string(abi.encodePacked(baseMessage, " and Derived logic"));
}
}What are Interfaces?
A Solidity Interface is like a blueprint or a contract without any implementation. It defines the public functions that a contract must have if it claims to implement that interface.
- They specify what functions exist and their parameters/return types.
- They do not contain any function bodies or state variables.
- All functions in an interface are implicitly
external.
Interface Syntax & Purpose
Interfaces are declared using the interface keyword. They are crucial for:
- Standardization: Ensuring contracts adhere to specific public APIs (e.g., ERC-20 token standard).
- Interoperability: Allowing contracts to interact with other contracts without knowing their full implementation details, only their interface.
- Modularity: Decoupling contract designs.
pragma solidity ^0.8.0;
interface ICalculator {
function add(uint a, uint b) external pure returns (uint);
function subtract(uint a, uint b) external pure returns (uint);
}Implementing an Interface
A contract can implement an interface using the is keyword, similar to inheritance. When a contract implements an interface, it must provide concrete implementations for all functions declared in that interface.
pragma solidity ^0.8.0;
interface IGreeter {
function greet() external view returns (string memory);
function setGreeting(string calldata _greeting) external;
}
contract MyGreeter is IGreeter {
string private currentGreeting = "Hello, World!";
function greet() public view override returns (string memory) {
return currentGreeting;
}
function setGreeting(string calldata _greeting) public override {
currentGreeting = _greeting;
}
}Quick Check: Inheritance vs. Interface
Which of the following statements about Solidity inheritance and interfaces is TRUE?
Recap: Inheritance & Interfaces
We've explored two powerful concepts in Solidity for building modular and reusable smart contracts:
- Inheritance: Allows a child contract to reuse code and logic from a parent using the
iskeyword. Functions can bevirtualin the parent andoverridein the child, andsupercan call parent logic. - Interfaces: Define a contract's public API without implementing its logic. They use the
interfacekeyword and ensure contracts adhere to specific function signatures, promoting standardization and interoperability.
Mastering these patterns is key to writing robust and maintainable Solidity code!
คำถามที่พบบ่อย
บทเรียน “การสืบทอดและอินเทอร์เฟซ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การสืบทอดและอินเทอร์เฟซ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Blockchain Smart Contracts with Solidity ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Blockchain Smart Contracts with Solidity มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การสืบทอดและอินเทอร์เฟซ”
ใช้การสืบทอดเพื่อให้โค้ดนำกลับมาใช้ซ้ำได้ และกำหนด API ของสัญญาให้ชัดเจนด้วยอินเทอร์เฟซ เพื่อการแบ่งส่วนโมดูลที่ดียิ่งขึ้น คุณปฏิบัติ Blockchain Smart Contracts with Solidity ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Blockchain Smart Contracts with Solidity หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Blockchain Smart Contracts with Solidity บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การสืบทอดและอินเทอร์เฟซ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Blockchain Smart Contracts with Solidity นี้ได้ไหม
ได้ บทเรียน Blockchain Smart Contracts with Solidity ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การสืบทอดและอินเทอร์เฟซ
- ไลบรารีและสัญญานามธรรม
- การจัดการข้อผิดพลาดด้วย Revert/Require
- ตัวปรับแต่งและรูปแบบตรวจสอบ-ผลกระทบ-การโต้ตอบ